About today

Node.js Code Coverage with Istanbul and Mocha

· by Otto Giron · Read in about 3 min · (534 Words)
CodeCoverage Istanbul Mocha Node.js

Code coverage is a measure of how much of your code has been  tested. Code coverage tools run a set of metrics in order to determine if your code has been completely tested, reducing the chance of  unwanted bugs.

You have to take into account that even if your code has 100% code coverage, that doesn’t guarantee all your tests are correct, there are some logical bugs you might miss, but as with anything, practice will help you with that.

Istanbul

Yet another JS code coverage tool that computes statement, line, function and branch coverage with module loader hooks to transparently add coverage when running tests.

Istanbul Github

Istanbul runs some  of the common coverage metrics/criteria to check whether your code meets the code coverage expectations. You can learn more about basic metrics here http://en.wikipedia.org/wiki/Code_coverage

Prerrequsites

Node.js >= 0.12, npm

You can also read my previous post Setting Up your Node.js Unit Tests with Chai And Mocha.

Steps

  • Create a new directory. In your terminal run:
mkdir mochaistanbul && cd mochaistanbul.
  • Run npm init.
npm init
  • Install Mocha, Chai and Istanbul:
npm install mocha chai istanbul --save-dev

  • Create  a module “index.js” in the root directory (mochaistanbul).
module.exports.getCoverageHello = function(to) {

return 'Hello, this is your first coverage test ' + to;
};

  • Create a test directory
mkdir test
  • Create a simple test file index.js under the test directory.
//test/index.js

/require chai and our simple 'index.js' module
require('chai').should();
//If you require an specific module, it will be included in the coverage test
var testModule = require('../index.js');

//simple test suite

describe('Istanbul Code Coverage', function(){

});
  • In your package.json file add (or replace) the following script.
...
"scripts": {
"test": "node ./node_modules/.bin/istanbul cover ./node_modules/.bin/_mocha -- --check-leaks"
},
...
  • Run ‘npm test’ in order to execute your test script.
npm test
=============================== Coverage summary ===============================
Statements   : 50% ( 1/2 )
Branches     : 100% ( 0/0 )
Functions    : 0% ( 0/1 )
Lines        : 50% ( 1/2 )
================================================================================

When you ran that command a new “coverage” folder was created, which contains a report of your code coverage. Check this file: mochaistanbul/coverage/lcov-report/mochaistanbul/index.js.html

Based on the report you can tell that one function is not being covered by the test suite. Let’s write a test in order to fix that

report.

  • Write a test for our function.
it('should return a hello message for an specific name', function() {
var name = 'otto';
var result = testModule.getCoverageMessage(name);
result.should.contain(name);
});
  • Run ‘npm test’ again
npm test
=============================================================================
Writing coverage object [/home/otto/workspace/tutorial/mochaistanbul/coverage/coverage.json]
Writing coverage reports at [/home/otto/workspace/tutorial/mochaistanbul/coverage]
=============================================================================

=============================== Coverage summary ===============================
Statements   : 100% ( 2/2 )
Branches     : 100% ( 0/0 )
Functions    : 100% ( 1/1 )
Lines        : 100% ( 2/2 )

The output reflect the new test we’ve implemented, so the code coverage is in 100%.

Conclusion

This was a simple example on how to get started with Code Coverage in Node.js. With Istanbul you can validate that your tests are covering your code based on specific metrics, which helps you  reduce the chance of defects. You should be aware that code coverage won’t guarantee your code is free of defects, but it’s a great tool for helping you with that.

Code available on github https://github.com/ottogiron/mochaistanbul

Comments