I am getting 0 % coverage 0 SLOC in mocha code coverage using blanket
Asked Answered
S

4

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');
    });
} )
Speaking answered 24/4, 2015 at 19:55 Comment(3)
I am having the exact same issue, did you ever fix this? My package.json script and config looks identical to yours.Prosser
I'm also having this exact problem.Rascality
This issue got fixed when I installed the latest node jsSpeaking
D
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

Dorinda answered 28/4, 2016 at 10:10 Comment(0)
K
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"
},
Khosrow answered 30/4, 2015 at 20:4 Comment(0)
R
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".

Refute answered 19/2, 2016 at 23:56 Comment(0)
T
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"

Tat answered 28/2, 2016 at 11:36 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.