Automated JavaScript testing with Mocha and js-coverage for NodeJS

I used to extensively use Jamine and Jasmine-Node for my JavaScript testing. While jasmine is great I’ve recently moved over to Mocha for several reasons but primarily because it supports coverage reporting.

Here are some basic steps to get going. There are several key components required, click on the different links and follow the installation instructions:

Setting up coverage

Tests must be configured to run with the instrumented code so that coverage works. The coverage command creates the instrumented version of the code in the lib-cov directory:

jscoverage lib lib-cov

The tests need to use these files instead. When using RequireJS (or node.js) this can be done by modifying index.js and using an environment variable. Calling the environment variable APP_COVERAGE (which you would have to set before calling the tests) it would look something like this:

module.exports = process.env.APP_COVERAGE ? require('./lib-cov/app') : require('./lib/app');

If there are multiple modules in a directory you can do something like this:

var module1 = process.env.APP_COVERAGE ? require('./lib-cov/module1'): require('./lib/module1');
var module2 = process.env.APP_COVERAGE ? require('./lib-cov/module2'): require('./lib/module2');

module.exports = {
   module1: module1,
   module2: module2
};

Then in the tests you can just require the modules as appropriate:

var app = require('../../index.js');

or

var module1 = require('../../index.js').module1;

Using Makefile

I have a standard Makefile that I use for running builds

Here’s a breakdown of the tasks/rules:

  • make test runs the tests and displays the results in the terminal. This is perfect for developer use
  • make test-ci runs the tests and creates an xUnit format XML report
  • make test-cov runs the tests and creates coverage report HTML file
  • make test-all runs the test-ci and test-cov tasks creates both the xUnit format XML report and the HTML coverage report. This is perfect for CI use

Showing coverage in Jenkins

If you have installed the Jenkins HTML Publisher Plugin and are using the make test-all command, you can then configure Jenkins to use the generated HTML coverage report by adding the “Publish HTML reports” post build action and configuring it to use the coverage.html file.

Live example

You can see an example of a fully working project in my HttpFileServerJS project (please note that this is subject to outages):

HttpFileServerJS GitHub project

HttpFileServerJS Jenkins job

Advertisements
Tagged with:
Posted in JavaScript, nodejs, programming, TDD, testing
3 comments on “Automated JavaScript testing with Mocha and js-coverage for NodeJS
  1. […] then I’ve been looking for alternatives to using JS Coverage for doing coverage instrumentation. […]

  2. The below link is address not active
    http://craigcook.co.uk/build/job/HTTP%20File%20Server

    moreover i tried to call the” make test-all” in execute script but the jenkins is thorwing error as ” make: command not found”
    I am unable to call the make command in any job what is the problem ..
    can u guide me ???

    Thanks,
    Jayakumar Natarajan

Leave a Reply

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

WordPress.com Logo

You are commenting using your WordPress.com 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 )

Google+ photo

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

Connecting to %s

%d bloggers like this: