Inline testing (vs. Rust)

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.

https://doc.rust-lang.org/book/ch11-03-test-organization.html

1 Like

This could be what you’re looking for: Inline tests in rescript - #10 by tsnobip.

Haven’t tried it in rescript, but I do use the ppx_inline_test referenced in pretty much all my OCaml projects.

2 Likes

Funny to have inline tests but not inline interfaces :upside_down_face:

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.

1 Like

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.

1 Like

You can do it with Vitest

3 Likes

any examples you could share to get an idea? I could imagine a format that works seamlessly with the new docgen headers.

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,
      "",
    )
  })
})

I don’t know if that’s what you expected.

Just added in-source testing support to rescript-vitest

5 Likes