Question

I am having trouble understanding how to unit test functions so that istanbul will consider them covered.

For an extremely simply example, I created this math module:

var add = function( one, two ){
    return one + two;
}

module.exports = {
    add: add
};

and basic unit testing:

var chai = require('chai');
var expect = chai.expect;

describe('add() method', function(){
    it('should do math', function(){
        expect( some.add(1, 1) ).to.equal(2);
    });
});

The report produced by istanbul gives me these results:

  • Statements: 66.67% (2 / 3)
  • Branches: 100% (0 / 0)
  • Functions: 0% (0 / 1)
  • Lines: 66.67% (2 / 3)
  • Ignored: none

enter image description here

If simply testing the result of calling the function doesn't count as coverage, how are you supposed to test this code when it is in a function scope your unit tests can't access?

Was it helpful?

Solution

As with all the toughest problems, this was caused by something stupid and unrelated.

I was running istanbul through grunt and was using the env workaround so the correct directory is used when checking coverage. As in something like this:

env: {
    coverage: {
        APP_DIR_FOR_CODE_COVERAGE: '../test/coverage/instrument/app/'
    }
},

As shown in the grunt-istanbul documentation

The problem was that I forgot to include env:coverage in the task I created for running the coverage tests.

Anyway, if you have this same problem you probably did something really stupid too.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top