Friday, April 22, 2016

Running cljs.test tests on node.js using doo and karma

Francesc and I are using doo and Karma to run our cljs.test tests on Node.js.

This is what we did to make it work.

First we installed Node.js, and then, Karma and its CLI following these instructions.

Once we had that, we added the doo plugin to the project.clj.

doo is a library and leiningen plugin that allows to run cljs.test on different JavaScript environments.

Next, we created a new namespace for the unit tests in the tests directory, and set up the test build in project.clj (we called the new namespace unit-tests):

Notice that in the setup you’ve to define which namespace will be used as the main of the unit tests: :main 'math-ops.unit-tests

In that namespace, we used doo to run the tests and register the namespaces containing unit tests:

Every time a new namespace containing unit tests is added, it has to be registered using the doo-tests macro in order to include it in the run.

Finally, to run the tests on Node.js we added it as a target to the build setup in project.clj:

Notice the addition of :target :nodejs to the compiler options.

Then we could run our tests writing lein doo node unit-tests on the console:

By default, doo watches the files in the source paths that we indicated in the build setup: :source-paths ["src/cljs" "test"], so any change to a file in them, makes the unit test run again.

You can check the code in this repository.


No comments:

Post a Comment