zth / reasonml-q-a

ReasonML Q & A - Questions and answers for everything ReasonML.
https://reasonml-q-a.netlify.com
44 stars 0 forks source link

What's the best way to test a Reason app? #21

Open mrispoli24 opened 4 years ago

mrispoli24 commented 4 years ago

Question

What's the best way to test a ReasonML app? Is it just to use jest and react testing library?

I'd imagine the process is to make a test.re file that compiles to a test.bs.js file that will run with the usual npm run jest, or are there baked in solutions for Reason and Reason React based code bases instead?

baransu commented 4 years ago

If you target JS and web I highly recommend glennsl/bs-jest. It's a binding library to Jest. Having proper setup based on that you can add react-testing-library bindings or write other tests which doesn't require any additional libraries.

If you target Native there is https://reason-native.com/docs/rely/ and mirage/alcotest.

Fusion commented 4 years ago

I was not aware of the existence of alcotest. Thanks for linking to it.

Regarding Reason Native with Rely:

For instance, in lib/dune:

(library
 (name YourLibraryPkg)
 (public_name your-library.lib)
 (ocamlopt_flags -linkall)
 (libraries lwt cohttp cohttp-lwt-unix whatever-you-need...)
)

In test/dune, you specify a library-executable pair and declare your module:

(library
   (name YourTests)
   (public_name your-tests.lib)
   (ocamlopt_flags -linkall -g)
   (libraries your-library.lib rely.lib )
   (modules (:standard \ RunTests))
)

(executable
 (package your-tests)
 (public_name RunTests)
 (name RunTests)
 (libraries your-tests.lib)
 (modules RunTests)
)

In test/RunTests.re, your main call:

YourTests.TestFramework.cli()

In test.TestFramework.re, your configuration:

include Rely.Make({
  let config =
    Rely.TestFrameworkConfig.initialize({
        snapshotDir: "./__snapshots__",
        projectDir: "./"
    });
});

And finally, in test/BogusTest.re (for instance: one of many tests!):

open TestFramework;

describe("a bogus test", ({test}) => {
...
});