My standard unit-testing tool for JavaScript is tape, which fits my needs very well. Most of the time these are run from the command line, like any other unit test, however some things are only possible with a browser around, which NodeJS is not.
tape itself is more than happy to run in a browser, but to get it there one needs to do either:
- Use tape-run to remotely control a web-browser and run the tests from the command line.
- Compile the unit test using browserify, then (re)load the page in your browser.
Setting up a browser to go with tape-run always seemed excessive to me, and was a fragile process. The latter generally requires a 2-step compile-on-the-command-line, refresh-the-browser process, which admittedly could be simplified with auto-watching scripts, but there's then the risk that you're not watching enough (or too many) files, you don't notice a syntax error because you're not looking at the watch window, etc.
Instead, I've created tape-server. This is a small module with minimal dependencies that runs a NodeJS web-server, which runs browserify on the target on demand (i.e. whenever you reload the page). Javascript console output is also redirected to the HTML page, so you don't have to have the console open to see success / failure.
As a result:
- There's no need to set-up headless browsers, or linkages from your container to your system's browser.
- Worry about compiling tests, and keeping compiled tests in-sync.
Usage
See the tape documentation for more information on writing tape tests themselves. Assuming you've written some you can add the following to package.json:
"scripts" : { "test-browser": "tape-server --port=8123 tests/test_*.js" }
Then run npm run test-browser and visit http://localhost:8123. Re-load the page, and your tests will run from freshly compiled source.