One thing I love about rust is the inline unit testing conventions, that you simple put a module tests at the end of your file together with a cfg(test) directive.
Is there a similar setup for ReScript? I would love to be able to do that and then just rescript test.
i wondered how does this even scale? Whenever I am writing tests for non trivial functions I end up writing a lot of code that I’d rather factor in a separate set of test files.
I believe doctests are meant to test examples in the documentation, so they don’t get stale. They can replace unit tests for simple cases, like utility libraries, or functions that does only one thing.
Organization wise, put one function per file or depend on IDE collapsing code.
We’ve used it quite extensively at work and never had scaling issues. The main point is not to use it for every tests but more for the ones that document the functions and mostly for functions that are not exported. This allows you to have interfaces that don’t get modified purely for testing purposes.
ok for example we have this middleware that handles basic auth, inside the module we have a function that verifies the basic auth and that is not exported, so we have this small inline test in it:
@inline_test
Zora.zora("basic auth", t => {
let valid = "Basic dXMzcjpwNHNz"
let invalid = "Basic dXMzcnA0c3M="
open Zora
t->test("valid header", t => {
t->equal(
verifyBasicAuth(~username="us3r", ~password="p4ss", ~header=Some(valid)),
#Success,
"valid",
)
})
t->test("wrong credentials", t => {
t->equal(
verifyBasicAuth(~username="usar", ~password="p4ss", ~header=Some(valid)),
#Failure,
"",
)
})
t->test("invalid header", t => {
t->equal(
verifyBasicAuth(
~username="us3r",
~password="p4ss",
~header=Some(invalid),
),
#Failure,
"",
)
})
})