JavaScript coverage with Istanbul and Coveralls via Travis CI

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 here
  • cat ./coverage/ | ./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:

  • 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.

Tagged with: , , , , ,
Posted in git, github, JavaScript, nodejs, TDD

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: