javascriptnode.jsmocha.jsblanket.js

Test coverage in Mocha with Blanket doesn't work


I have a Node app I'd like to test, and get coverage report. I followed the Getting Started Guide but it doesn't seem to work.

My source, in src/two.js:

var two = 1 + 1;
module.exports = two;

My test, in test/two.js:

var expect = require('expect');
var two = require('../src/two');

describe('two', function() {
  it('should be 2', function(done) {
    expect(two).toBe(2);
    done();
  });
});

And my package.json:

{
  "scripts": {
    "test": "mocha",
    "cover": "mocha -r blanket -R html-cov > coverage.html"
  },
  "devDependencies": {
    "blanket": "^1.2.1",
    "expect": "^1.13.4",
    "mocha": "^2.3.4"
  }
}

When I run npm run test everything works as you'd expect. But when I run npm run cover, in my coverage.html file I get 0% coverage 0 SLOC and nothing else.


Solution

  • I got it to run by adding this to package.json:

    ...
      "config": {
        "blanket": {
          "pattern": "src",
          "data-cover-never": ["node_modules"]
        }
      },
    ...
    

    Apparently Blanket does not default to src despite what the guide says (there's an old open issue on GitHub).

    It also tries to cover all of the paths that match the pattern, not just the src dir, so in this case it tried to cover external files as well (node_modules/has/src/index.js, which was installed by Expect). I had to add the data-cover-never to avoid it.