20

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.

Faraz
  • 6,025
  • 5
  • 31
  • 88
  • 3
    [Use the `--all` flag to include files that have not been required in your tests](https://github.com/istanbuljs/nyc#use-the---all-flag-to-include-files-that-have-not-been-required-in-your-tests) – laggingreflex May 22 '18 at 10:33
  • 1
    it didn't work.. – Faraz May 24 '18 at 02:22

6 Answers6

11

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

mckoss
  • 6,764
  • 6
  • 33
  • 31
8

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": []
}
Lee Goddard
  • 10,680
  • 4
  • 46
  • 63
Stéphane de Luca
  • 12,745
  • 9
  • 57
  • 95
4

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

lbragile
  • 7,549
  • 3
  • 27
  • 64
Clinton Roy
  • 2,659
  • 2
  • 10
  • 7
3

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

Adam Reis
  • 4,165
  • 1
  • 44
  • 35
3

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.

Drazisil
  • 3,070
  • 4
  • 33
  • 53
1

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

Siva Kannan
  • 2,237
  • 4
  • 27
  • 39