LCOV reporter for Mocha.
The mocha-lcov-reporter is a reporter for mocha. In order to get coverage data, the same instructions are to be followed as for the
- Install jscover or node-jscoverage
- Instrument your library with
- Run your tests against your instrumented library and save the output
For example, the following script can be part of your build process (add
devDependencies to your
package.json file and run
rm -rf coverage
rm -rf lib-cov
node_modules/.bin/jscover lib lib-cov
mv lib lib-orig
mv lib-cov lib
node_modules/.bin/mocha -R mocha-lcov-reporter > coverage/coverage.lcov
rm -rf lib
mv lib-orig lib
This script instruments your sources (source 'lib', target 'lib-cov'), temporarily replaces your library by the instrumented version, run the tests against the instrumented version of your sources, and undoes the replacing of the original library by the instrumented library.
A safer and better approach is to instrument your library (target 'lib-cov'), and include that directory from your tests directly. Instead of doing a 'require("../lib")' do a 'require("../lib-cov")'. This saves the hassle of replacing directory 'lib' with directory 'lib-cov' and undoing it afterwards. You can use an environment variable to check if the instrumented library should be included or the normal version:
var lib = process.env.JSCOV ? require('../lib-cov') : require('../lib');
And to get the test-coverage, run mocha as follows:
JSCOV=1 mocha -R mocha-lcov-reporter > coverage/coverage.lcov
See the SaXPath project for an example on how to do this.
Incomplete paths in LCOV output
Unfortunately, when the code is instrumented using
node-jscoverage, the output of the reporter will have incomplete paths for the covered files. A quick fix is to update the paths after running the tests with the mocha-lcov-reporter, like so:
# run the tests
JS_COV=1 ./node_modules/.bin/mocha -R mocha-lcov-reporter > coverage/coverage_temp.lcov
# fix the paths
sed 's,SF:,SF:lib/,' coverage/coverage_temp.lcov > coverage/coverage.lcov
The reason this is that
jscover runs on the directory you specify (e.g.,
lib/) and regards that as the root for the project.
Blanket.js can be used as well. After the lcov file, be sure to fix the paths for the covered files. The path will be an URL, having
file: as its protocol. Using the same manner as above, the path can be fixed using
What does LCOV output look like? LCOV is meant to be interpreted by other programs and not meant to be readable by humans. This is an example:
If you are looking for something human readable, the
HTMLCov reporter can be used.