Components used: Mocha, Istanbul, Coveralls, Travis, node coveralls, make
Since then I’ve been looking for alternatives to using JS Coverage for doing coverage instrumentation. There are two main issues with it:
- It’s C component that needs to be (sort of manually) installed
- It requires you to implement a mechanism within your modules exports to be able to switch between the the instrumented code and the original code (usually done with a runtime variable i.e.
PKG_COV=1
)
The second issue is the bigger pain as I want the coverage component to do all that for me. After some digging around and experimentation I decided upon Istanbul as the component to use. Mainly because it’s written in pure JavaScript and it seamlessly integrates with your existing build.
Before starting it’s worth understanding a little bit about how it integrates with your code. It works by having hooks hooks into where your code includes it’s libs. The two hooks are:
require
vm.createScript
If your tests don’t include your lib files via this mechanism then the instrumentation won’t work. It’s therefore worth noting that your codebase must conform to CommonJS i.e. the lib files must do module.export = Lib
or exports.Lib = Lib
etc. I’m still looking for a way of doing the instrumentation when require is not used and if I find something I’ll write an update.
Getting it working
Key commands
istanbul cover _mocha -- -R spec test/spec
– the key here is to note that_mocha
is being used. This is due to the way that the process is being spawned see herecat ./coverage/lcov.info | ./node_modules/coveralls/bin/coveralls.js
– this takes the coverage report generated by the previous command and pushes it to coveralls
There are basically a few files you need to modify:
before_script: 'npm install -g istanbul && npm install -g mocha' | |
script: 'make test-cov' | |
after_success: 'make coveralls' |
test-cov: istanbul | |
istanbul: | |
istanbul cover _mocha — -R spec test/spec | |
coveralls: | |
cat ./coverage/lcov.info | ./node_modules/coveralls/bin/coveralls.js |
"devDependencies": { | |
"coveralls": "2.3.0" | |
} |
Notes
- I’m using mocha as a test framework but any other can be used.
- I’m assuming you’re already familiar with travis and coveralls and haven’t covered any setup information here.
- I’m using Make but it’s not required. The commands are the same though.
Examples
https://github.com/BoyCook/SpaceUI
https://github.com/BoyCook/GitAll