7

I am trying to get the code coverage in MOCHA JS test. I am using the blanket and the but I am getting 0 % coverage 0 SLOC why I am not understanding. my package.json is

{
  "name": "basics",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "mocha && mocha test --require blanket --reporter html-cov > coverage.html"
  },
  "author": "",
  "license": "MIT",
  "devDependencies": {
    "chai": "~2.2.0",
    "mocha": "~2.2.4",
    "blanket": "~1.1.6",

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

and index.js is

exports.sanitize = function(word){


    return word.toLowerCase().replace(/-/g, ' ');
}

exports.testToString = function(){


    return word.toLowerCase().replace(/-/g, ' ');
}

and indexSpec.js which is under test folder is

var chai = require('chai');
var expect = require('chai').expect;
var word = require('../index.js');

describe ('sanitize', function(){
    it('String matching ', function(){

        var inputWord = 'hello WORLD';
        var outputWord = word.sanitize(inputWord);
        expect(outputWord).to.equal('hello world');
        expect(outputWord).to.not.equal('HELLO WORLD');
        expect(outputWord).to.be.a('string');
        expect(outputWord).not.to.be.a('number');

    });

    it('Checke hyphen ', function(){
        var inputWord = 'hello-WORLD';
        var outputWord = word.sanitize(inputWord);
        expect(outputWord).to.equal('hello world');
    });
} )
user993158
  • 79
  • 5

4 Answers4

2

Paul is right. There is no point in using buggy library. Steps for making this code work with Istanbul:

  1. Install Istanbul globally npm install -g istanbul
  2. Change script section in package.json
  "scripts": {
    "test": "mocha",
    "coverage": "istanbul cover node_modules/mocha/bin/_mocha -- -R spec"
  },
  1. Run test by typing npm test

  2. Generage coverage report: npm run coverage

Coverage report will be available in coverage/lcov-report/index.html

CherryDT
  • 13,941
  • 2
  • 31
  • 50
user2811588
  • 323
  • 2
  • 5
0

Get blanket from the git repo. I don't know what's wrong with their npm package, but it didn't work for me either.

Getting the module from git repo works fine.

Make the below changes in your package.json file

"devDependencies": {
    "chai": "~2.2.0",
    "mocha": "~2.2.4",
    "blanket": "git://github.com/alex-seville/blanket.git"
},
Jaydeep Solanki
  • 2,575
  • 5
  • 33
  • 48
0

I had this problem and added a blanket.js file in my root directory as recommended by Neilskrijger here ... https://github.com/alex-seville/blanket/issues/361 . I then set my blanket pattern in my package.json to '/lib' which was the root of my source code and it worked. The forward slash was required. My test script was "mocha --require blanket -R html-cov --recursive> coverage.html".

escapedcanadian
  • 163
  • 2
  • 9
0

It seems like a lot of us have used the same tutorial and met the same problem.

I have tried all hints given on this page (tried with node versions: node-v4.3.1 and node-v5.7.0) + a few more without any luck I ended up with another package Istanbul, which I should have done from the start since I normally use the stats as a indicator of which package to use (Its used by so many more users). First try with this package and it worked :-) I added this to the script section of package.json:

"coverage" : "./node_modules/.bin/istanbul cover ./node_modules/mocha/bin/_mocha -- -R spec"

Plaul
  • 4,326
  • 4
  • 13
  • 18