Faraz
Faraz

Reputation: 6275

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.

Upvotes: 21

Views: 21465

Answers (6)

mckoss
mckoss

Reputation: 7034

I hit this issue when using "type": "module" (ES Modules) with TypeScript.

There is an issue with the nyc (Instanbul) loader for ESM modules. The recommended solution is to abandon nyc and use the native coverage now built into Node 13 and above: c8.

https://github.com/bcoe/c8

Upvotes: 16

Stéphane de Luca
Stéphane de Luca

Reputation: 13621

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": []
}

Upvotes: 9

Drazisil
Drazisil

Reputation: 3363

TL;DR: Try useSpawnWrap: true in your nyc config.


The root cause of the issue is due to the way nyc instruments the files for coverage. It does this by watching it's child process and keeping track of what code is ran. This is why you pass mocha to nyc

You can test if this is the case by adding the --show-process-tree flag to nyc, as described in the nyc docs. If you see "nyc" at the bottom of the output, but nothing under it, you will know this is your issue.

Now, the note above explains how to work around this (useSpawnWrap: true), but what causes it? This GitHub issue comment partly explains what is happening, and this comment explains how pnpm broke this ability. In my case I am hitting this with nvm, though I sure it is the same root cause, something is affecting either path, or the process.env.NODE_OPTIONS environmental variable.

I hope this more detailed explanation helps you troubleshoot issues like this in the future.

Upvotes: 3

Clinton Roy
Clinton Roy

Reputation: 2869

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 😄

Upvotes: 4

Adam Reis
Adam Reis

Reputation: 4483

In my case, the problem was that .nyrc configuration contained "instrument": false. Removing this brought back coverage reports.

Upvotes: 3

Siva Kannan
Siva Kannan

Reputation: 2481

My case is a little different,

I have installed mocha globally and when nyc referring mocha globally it does't show anything in the coverage report.

I got result when running

nyc node_modules/.bin/mocha

It is a known issue and refer here https://github.com/istanbuljs/nyc/issues/1029#issuecomment-514174340

Upvotes: 1

Related Questions