Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

NYC output is empty #822

Closed
hayderma opened this issue Apr 26, 2018 · 37 comments
Closed

NYC output is empty #822

hayderma opened this issue Apr 26, 2018 · 37 comments

Comments

@hayderma
Copy link

hayderma commented Apr 26, 2018

I have been trying for hours to find the problem here, but I can't, I tried many things, this is my test file (unit-test.js) inside projectRoot/test

var should = require('should');


var arr = [1, 2, 3];
function ap(ar) {
  a = ar;
  var L = a.length;
  if (L > 1) {
    a[L] = 4;
  }
  console.log(a);
  return a;
}

describe('#indexOf()', function () {
  it('should return -1 when the value is not present', function () {
    if (arr.length > 1) {
      should.equal(ap(arr)[3], 4);
      //should.equal(arr[2],3);
    }
  });
});

And this is my package.json:

{
  "name": "jsonave-test",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "lint": "eslint .",
   "test": " mocha test/unit-test.js --check-leaks --recursive --exit",
    "cover": "nyc npm test"
  },
  "author": "",
  "license": "ISC",
  "dependencies": {
    "http": "0.0.0",
    "jsonapter": "^2.0.3",
    "jsonave": "0.0.3",
    "moment": "^2.21.0",
    "nock": "^9.2.3",
    "should": "^13.2.1"
  },
  "devDependencies": {
    "eslint": "^4.19.1",
    "nyc": "^11.7.1"
  }
}

when i run , the output shows coverage all 0% and no file name is mentioned, as shown below

$ npm run cover

> [email protected] cover C:\Users\h\WebstormProjects\jsonave-test
> nyc mocha



  #indexOf()
[ 1, 2, 3, 4 ]
    √ should return -1 when the value is not present


  1 passing (0ms)

----------|----------|----------|----------|----------|-------------------|
File      |  % Stmts | % Branch |  % Funcs |  % Lines | Uncovered Line #s |
----------|----------|----------|----------|----------|-------------------|
All files |        0 |        0 |        0 |        0 |                   |
----------|----------|----------|----------|----------|-------------------|

@JaKXz JaKXz added the question label May 1, 2018
@JaKXz
Copy link
Member

JaKXz commented May 1, 2018

This is because nyc's instrumentation /reporting works based the files you require (or import) in your tests. If you split unit-test.js into two files, one with the test and the other with the test subject, it should list the test subject file in the output. :)

That explanation is an extreme paraphrase btw, there's a better one in the README and other places.

@redonkulus
Copy link

redonkulus commented May 3, 2018

@JaKXz I'm getting the same issue, here is the stub code (after trying to add a stub.js file as you mentioned above):

// test.js
const assert = require('assert');
const stub = require('./stub');
it('should have tests', () => assert.equal(stub, true));
// stub.js
module.exports = true;
// .nycrc
{
  "sourceMap": false,
  "instrument": false
}
$ ./node_modules/.bin/nyc ./node_modules/.bin/mocha tests/unit/index.js


  ✓ should have tests

  1 passing (11ms)

----------|----------|----------|----------|----------|----------------|
File      |  % Stmts | % Branch |  % Funcs |  % Lines |Uncovered Lines |
----------|----------|----------|----------|----------|----------------|
All files |  Unknown |  Unknown |  Unknown |  Unknown |                |
----------|----------|----------|----------|----------|----------------|

@JaKXz
Copy link
Member

JaKXz commented May 4, 2018

Why are you setting instrument: false?

@redonkulus
Copy link

@JaKXz My project which uses babel had that set already. Even in this simple test, turning it to true doesn't collect coverage. I've uploaded the files to a repo so you can try it out if you want.

https://github.com/redonkulus/nyctest

@JaKXz
Copy link
Member

JaKXz commented May 4, 2018

Interesting. Try this tutorial: https://istanbul.js.org/docs/tutorials/es2015/

Coverage with babel should be no problem.

@JaKXz
Copy link
Member

JaKXz commented May 4, 2018

I'm really confused by this because all I did to get your example repo working was remove .nycrc

@redonkulus
Copy link

I'm really confused by this because all I did to get your example repo working was remove .nycrc

So is that a bug in how nyc is processing the config file?

@redonkulus
Copy link

@JaKXz ping

@JaKXz
Copy link
Member

JaKXz commented May 16, 2018

I think this issue should be re-phrased as a bug, but the bug is probably in yargs not NYC.

Please close this issue and open one there.

For now I hope you have an adequate workaround.

@redonkulus
Copy link

Ok thanks. I have no work around yet.

@sibajeeray
Copy link

The problem is, Mocha is not exiting after its execution. So coverage report is unable to print as the execution is hold by mocha.
In Script of package.json mention --exit after mocha.
EX- nyc --reporter=html --reporter=text mocha --exit

@hayderma
Copy link
Author

hayderma commented Jul 4, 2018

I have not worked on that NYC since opening this issue, for those who still have their projects running, if @sibajeeray answer solves the issue, feel free to close it, else I will try something on it in the next couple of days.

@technowhiz
Copy link

The problem is, Mocha is not exiting after its execution. So coverage report is unable to print as the execution is hold by mocha.
In Script of package.json mention --exit after mocha.
EX- nyc --reporter=html --reporter=text mocha --exit

bro!!!!!!!, thanks a lot, i was worrying about how to end the mocha process after the tests has finished and how to display the coverage report. your solution solved my problems... thanks a lot once again

@75lb
Copy link

75lb commented Jan 2, 2019

I have the same issue, reproduced on macOS High Sierra using node v6.14.4 and v11.4.0.

In an empty directory, install the deps.

$ npm i mocha nyc
$ npm ls --depth 0
[email protected] /Users/llobrook/Documents/tmp/broken-nyc
├── [email protected]
└── [email protected]

Create this file test.js:

it('one', function () {
  return 'one'
})

it('two', function () {
  return 'two'
})

Run mocha with nyc.

$ npx nyc mocha test.js


  ✓ one
  ✓ two

  2 passing (8ms)

----------|----------|----------|----------|----------|-------------------|
File      |  % Stmts | % Branch |  % Funcs |  % Lines | Uncovered Line #s |
----------|----------|----------|----------|----------|-------------------|
All files |        0 |        0 |        0 |        0 |                   |
----------|----------|----------|----------|----------|-------------------|

As you can see, the report is empty. Any ideas?

@75lb 75lb mentioned this issue Jan 2, 2019
@quentinvernot
Copy link

quentinvernot commented Jan 2, 2019

@75lb I added a second file to your repro, I get coverage when I do that:

lib.js:

module.exports = { inc: x => x + 1 };

test.js:

const { inc } = require('./lib');
it('1', () => inc('1'))

Result:

$ npx nyc mocha test.js


  ✓ 1

  1 passing (6ms)

----------|----------|----------|----------|----------|-------------------|
File      |  % Stmts | % Branch |  % Funcs |  % Lines | Uncovered Line #s |
----------|----------|----------|----------|----------|-------------------|
All files |      100 |      100 |      100 |      100 |                   |
 lib.js   |      100 |      100 |      100 |      100 |                   |
----------|----------|----------|----------|----------|-------------------|

Maybe nyc doesn't provide coverage inside the test files by default?

@75lb
Copy link

75lb commented Jan 2, 2019

Maybe nyc doesn't provide coverage inside the test files by default?

OK, makes sense although I had the same empty results even when I tested separate files.

I figured out my issue. I discovered a node_modules/.cache directory - deleting this made my coverage report work again. The previous committer had set the instrument: false flag in the nyc config. I noticed this and suspected it could be causing the empty results issue so deleted the flag but nyc still produced empty results, hence my confusion (I didn't know about the cache).

@JaKXz
Copy link
Member

JaKXz commented Jan 3, 2019

We should add to the troubleshooting steps to add cache: false somewhere in your nyc config when trying to reproduce. cc @bcoe [where do you think is best, the Issue template?]

Is everyone in this thread satisfied enough to close this issue? Perhaps a PR for 👆? :)

@AnanyaJha
Copy link

AnanyaJha commented Feb 5, 2019

I have a similar problem where I'm not able to view any tests in the coverage summary.
So far, I have:

test:vscode-integration": "node ../../scripts/run-vscode-integration-tests-with-top-level-extensions --reporter mocha-multi-reporters --reporter-options configFile=../../config/mochaConfig.json

the flags I have specified are:

  "nyc": {
    "reporter": ["text-summary", "lcov"],
    "temp-directory": "./coverage/.nyc_output",
    "all": true,
    "exclude": "typings",
    "extension": [".ts"],
    "sourceMap": true,
    "instrument": true,
    "include": ["src/**/*.ts", "src/**/*.tsx"]
  }

in the actual test script run-vscode-integration-tests-with-top-level-extensions I have:

shell.exec(
  `cross-env CODE_VERSION='1.30.0' ./node_modules/.bin/nyc cross-env CODE_EXTENSIONS_PATH='${path.join(
    __dirname,
    '..',
    'packages'
  )}' CODE_TESTS_WORKSPACE='${path.join(
    __dirname,
    '..',
    'packages',
    'system-tests',
    'assets',
    'sfdx-simple'
  )}' CODE_TESTS_PATH='${path.join(
    cwd,
    'out',
    'test',
    'vscode-integration'
  )}' node ./node_modules/vscode/bin/test ./node_modules/.bin/_mocha --exit`
);

and my output is

**lists all the tests here**
15 passing (57ms) 
Tests ended with 0 failure(s)
Existing test process, code should be 0
Tests exited with code: 0
=============================== Coverage summary ===============================
Statements: 0% ( 0/9 )
Branches  : 0% ( 0/7 )
Functions : 0% ( 0/1 )
Lines        : 0% ( 0/9 )
================================================================================ 

It looks like it's running all the tests using the node test script I provided and mocha is being invoked internally through the vscode 'testrunner', so I'm confused as to what's going on. Additionally, I do see files generated in the .nyc_output folder but the files are empty, and the lcov.info files have half data as well, so I'm assuming they aren't writing correctly. I'm running these tests through vscode with typescript. Any help would be appreciated, thanks!

@FraBle
Copy link

FraBle commented Feb 6, 2019

I currently experience the same issue as reported by @AnanyaJha

Edit: I resolved my issue. I had a custom NODE_ENV for Jenkins, but it was not reflected in the babel settings in my package.json.

Before:

...
  "babel": {
    "env": {
      "test": {
        "plugins": [
          "istanbul"
        ]
      },
    },
  },
...

After:

...
  "babel": {
    "env": {
      "test": {
        "plugins": [
          "istanbul"
        ]
      },
      "jenkins": {
        "plugins": [
          "istanbul"
        ]
      }
    },
  },
...

@Antonius-S
Copy link

Antonius-S commented Feb 6, 2019

The same issue. Reproduced with stub.js from this post and nyc installed globally. In my case issue seems to happen only with global mocha:

D:\Projects\nyc-test>nyc node_modules\.bin\mocha


  √ should have tests

  1 passing (39ms)

----------|----------|----------|----------|----------|-------------------|
File      |  % Stmts | % Branch |  % Funcs |  % Lines | Uncovered Line #s |
----------|----------|----------|----------|----------|-------------------|
All files |      100 |      100 |      100 |      100 |                   |
 stub.js  |      100 |      100 |      100 |      100 |                   |
----------|----------|----------|----------|----------|-------------------|

D:\Projects\nyc-test>nyc mocha


  √ should have tests

  1 passing (39ms)

----------|----------|----------|----------|----------|-------------------|
File      |  % Stmts | % Branch |  % Funcs |  % Lines | Uncovered Line #s |
----------|----------|----------|----------|----------|-------------------|
All files |        0 |        0 |        0 |        0 |                   |
----------|----------|----------|----------|----------|-------------------|

NB: Curiously, the presence of option "all" either in arguments or in .nycrc file seems to change some things a bit:

----------|----------|----------|----------|----------|-------------------|
File      |  % Stmts | % Branch |  % Funcs |  % Lines | Uncovered Line #s |
----------|----------|----------|----------|----------|-------------------|
All files |        0 |      100 |      100 |        0 |                   |
 stub.js  |        0 |      100 |      100 |        0 |                 2 |
----------|----------|----------|----------|----------|-------------------|

though coverage info still weird.
lcov.info when a call is correct (local mocha):

TN:
SF:D:\Projects\nyc-test\stub.js
FNF:0
FNH:0
DA:2,1
LF:1
LH:1
BRF:0
BRH:0
end_of_record

lcov.info for half-working call (global mocha & "all" option):

TN:
SF:D:\Projects\nyc-test\stub.js
FNF:0
FNH:0
DA:2,0
LF:1
LH:0
BRF:0
BRH:0
end_of_record

@AnanyaJha
Copy link

@Antonius-S were you able to get your setup working?

@Antonius-S
Copy link

Antonius-S commented Feb 28, 2019

@AnanyaJha kind of. I dislike the fact I need tons of mocha packages inside each of my tiny module's folder so I installed mocha from node's local folder (npm i <node>/node_modules/mocha). This way npm sets shortcut to that folder so I just have one little link in each of my module's node_modules folder. And it makes nyc work as expected!

@Antonius-S
Copy link

Found one more workaround that doesn't use softlinks. Solution is for Windows and globally installed mocha, path to Node must be in PATH and mocha must be in %Node%\node_modules\mocha. Of course you can change prerequisites by changing the script.

Copy the text below and save as %yourproject%\node_modules\.bin\mocha.cmd

:: This batch launches globally installed mocha entry point

@ECHO OFF
SETLOCAL

SET NodeDir=
FOR /F %%S IN ('where node.exe') DO SET NodeDir=%%~dpS
IF NOT DEFINED NodeDir ECHO Node.exe not found in path && PAUSE && GOTO :EOF

node  "%NodeDir%\node_modules\mocha\bin\mocha" %*

@bcoe
Copy link
Member

bcoe commented Mar 12, 2019

there are a variety of reasons why coverage might be blank, and this thread seems to not be related to one specific cause. I'm closing this issue, but folks should feel free to open issues more specific to the problems they're bumping into; ideally with a minimal reproduction in a repo.

@bcoe bcoe closed this as completed Mar 12, 2019
@bcoe bcoe added the triaged label Mar 12, 2019
@Antonius-S
Copy link

@bcoe what do you think about my case (no coverage with global mocha but local mocha works OK)? Is it NYC issue or mocha's? Should I report it separately?

@bcoe
Copy link
Member

bcoe commented Mar 13, 2019

@Antonius-S feel free to open an issue 👍 just want to make sure we're very specific about how to reproduce; these catch all issues almost never get solved, because there are multiple causes of empty coverage.

@winnieBear
Copy link

We should add to the troubleshooting steps to add cache: false somewhere in your nyc config when trying to reproduce. cc @bcoe [where do you think is best, the Issue template?]

Is everyone in this thread satisfied enough to close this issue? Perhaps a PR for ? :)

maybe you can add 'cache-dir=your_cache_dir', so you can fix and get result quickly.

@arjunpanicker
Copy link

The problem is, Mocha is not exiting after its execution. So coverage report is unable to print as the execution is hold by mocha.
In Script of package.json mention --exit after mocha.
EX- nyc --reporter=html --reporter=text mocha --exit

Thank you. This worked like a charm.

@diiiego83
Copy link

Hi had the same issue. mocha works correctly but nyc output and empty report.

"scripts": {
    "test": "mocha ./dist/utest/test.js --exit",
    "coverage": "nyc npm test",
  },  
  "nyc": {
    "include": [
      "dist/**/*.js"
    ],
    "reporter": [
      "text-summary",
      "html",
      "cobertura"
    ],
    "sourceMap": true,
    "instrument": true
}
=============================== Coverage summary ===============================
Statements   : Unknown% ( 0/0 )
Branches     : Unknown% ( 0/0 )
Functions    : Unknown% ( 0/0 )
Lines        : Unknown% ( 0/0 )
================================================================================

@coreyfarrell
Copy link
Member

@diiiego83 Having the same symptom does not mean you have the same issue (cause). Please post a new issue including all required information so we can troubleshoot your issue.

@davegildea
Copy link

I had this issue running in cmd however when I changed to using Ubuntu on WSL it worked a treat. So the process not exiting correctly appears to cause the issue with nyc not getting the reports

@ZaidRehman
Copy link

ZaidRehman commented Mar 11, 2021

Still facing this issue. I have tried all the above-mentioned solution.

@way2ex
Copy link

way2ex commented Jul 6, 2021

Still facing this issue. I have tried all the above-mentioned solution.

I have found if i test compiled code in dist and typescript has generate sourceMap, i must include both dist and src.
Have a try.

@Imperio8
Copy link

Maybe nyc doesn't provide coverage inside the test files by default?

My folder was called "test" so it skipped everything. Thank you.

@sayo96
Copy link

sayo96 commented Apr 25, 2023

The --exit option did not work for me. Is there any alternate fixes for this

In my case the tests are within the src folder and i'm running nyc command like:
nyc mocha --recursive ./src.

The tests are passing but the coverage report is empty!
Screenshot 2023-04-25 at 1 59 35 pm

@alecgibson
Copy link

In case this helps any future travellers: our coverage broke when I declared a variable var arguments = [].

arguments isn't a reserved word (which is why tests correctly pass), but does have special meaning, and maybe this was tripping up nyc. Would have been nice to have had some warning about this.

@tharindug99
Copy link

> [email protected] test
> nyc --all mocha "test/*.spec.js"

Initiating Functions


  Basic Calculations
    ✔ should add 1 and 2 to get 3
    ✔ should add 5 and 5 to get 10
    ✔ should subtract 5 from 10 to get 5
    ✔ should subtract 8 from 20 to get 12
    ✔ should multiply 2 and 3 to get 6
    ✔ should multiply 4 and 5 to get 20
    ✔ should divide 10 by 2 to get 5
    ✔ should divide 20 by 4 to get 5


  8 passing (7ms)

------------|---------|----------|---------|---------|-------------------
File        | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s
------------|---------|----------|---------|---------|-------------------
All files   |       0 |        0 |       0 |       0 | 
 myClass.js |       0 |        0 |       0 |       0 | 3-30
------------|---------|----------|---------|---------|-------------------

Output is shown as empty.

{
  "name": "mocha",
  "version": "1.0.0",
  "main": "index.js",
  "scripts": {
    "test": "nyc --all mocha \"test/*.spec.js\" "
  },
  "author": "",
  "license": "ISC",
  "description": "",
  "devDependencies": {
    "mocha": "^10.7.3",
    "nyc": "^17.1.0"
  },
  "type": "module",
  "dependencies": {
    "chai": "^5.1.2",
    "source-map-support": "^0.5.21"
  },
  "nyc": {
    "include": [
      "src/**/*.js"
    ],
    "exclude": [
      "Data/*.js"
    ]
  }
}

Please help to fix this

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests