NodeJS Test Coverage Reporting with CoverShot
Automated testing is a critically important practice at Near Infinity. We try to automate every test we can and run them all the time as part of a continuous integration process. But how do you know if you're tests are any good? One method for doing so is reporting on your test code coverage.
If you're using Node.js, there are several tools for instrumenting your code for coverage analysis, but none provide a great output format for easily viewing and navigating the results. That's why we decided to create CoverShot.
CoverShot is a multi-format, test framework agnostic, code coverage report generator for Node.js applications and modules. That's a mouthful for saying it's a tool that generates nice code coverage output in several formats, no matter which test framework you use. A sample of the HTML summary page is shown below.
Some of the most interesting features of CoverShot include:
- Test Framework Agnostic - It doesn't matter which test framework you use. In fact, your test framework won't even know that code coverage is being collected.
- Multi-process Support - Coverage will be collected even on code executed by spawned child processes.
- Not Tied to Testing - There's nothing about CoverShot that requires coverage collection to be initiated by tests. You can even use it to record coverage from your running application.
To get started, you'll need to install
covershot and its dependencies.
$> npm install covershot $> npm install jsmeter2
The first step to beautiful code coverage reports is to instrument your
$> jscoverage --no-highlight lib lib-cov
Next, you'll need to change the way you require the files you want to collect coverage on.
var csrequire = require('covershot').require.bind(null, require); // coverage will be collected for this file and all files it requires var myLibrary = csrequire('../lib/myLibrary');
If you'd like to collect some source code metrics too, run
$> ./node_modules/node-jsmeter/bin/jsmeter.js -o ./covershot/jsmeter/ ./lib/
covershot to generate the report, in this case an HTML
$> ./node_modules/covershot/bin/covershot covershot/data -f html
A static set of HTML pages will be generated in the covershot subdirectory of your current working directory.
It's important to realize that good test coverage does not guarantee error-free code. You can still fail with 100% test coverage. It's simply another tool in the toolbox for helping you evaluate the effectiveness of your tests. Please check it out and let us know how we can improve CoverShot.