Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

No coverage nyc mocha

I am just unable to figure out why test coverage is 0 even though test case is passing. I have a script in package.json:

"nyctest": "node --max_old_space_size=4096 node_modules/nyc/bin/nyc.js --reporter=text mocha"

When I run npm run nyctest

My test pass, but coverage is 0 percent.

enter image description here

Following is the test and the file is it testing:

test.js

var chai = require('chai');
var sinon = require('sinon');
var sinonChai = require('sinon-chai');
chai.should();
chai.use(sinonChai);
var application = require('../../../src/main/resources/static/js/components/app.js');

describe('sample return testing', function(){
    it('should return true', function(){
        application.sample.returnValue().should.equal(true);
    })
});

app.js

const sample = {
    returnValue: function () {
        return true;
    }
};

module.exports = {sample};

Appreciate any help.

like image 280
Faraz Avatar asked May 22 '18 04:05

Faraz


People also ask

What is NYC in package JSON?

nyc is a command line tool for instrumenting code with Istanbul coverage (the successor to the istanbul command line tool). Let's see how we can't integrate nyc into our build and try it out!

Does NYC work with jest?

Jest --coverage works for our unit tests and correctly reports coverage, so we will temporarily use Jest to cover unit tests and NYC to cover integration tests.

What is NYC mocha?

Mocha, a javascript test framework. So istanbul/nyc is a code coverage tool which works well with mocha, they are simple and easy tools that make testing easy. We can install mocha by typing this code in our terminal. We install istanbul with.


2 Answers

By August 2020: two things:

  1. You need to add the --all parameter.
  2. Preferably use the .nycrc file in order to not have a long command line in your package.json as follows:

The minimal .nycrc file:

{
  "all": true,
  "include": [
    "test/**.js"
  ],
  "exclude": []
}
like image 60
Stéphane de Luca Avatar answered Oct 10 '22 22:10

Stéphane de Luca


You need to modify your nyctest command with --all flag, as follows:

"nyctest": "nyc --reporter=lcov --reporter=text-lcov --all -x \"./node_modules/*\" -x \"./coverage/*\" check-coverage --lines 10 --functions 90 npm run unittest"

So with --all flag, all files are fetched 😄

like image 20
Clinton Roy Avatar answered Oct 10 '22 23:10

Clinton Roy