nyc icon indicating copy to clipboard operation
nyc copied to clipboard

Generate report from a JSON file merged from `nyc merge`

Open ansjcy opened this issue 4 years ago • 2 comments

I have multiple coverage results from different types of tests (regression tests: node processes instrumented by nyc, jest tests: can produce coverage json results). I run nyc merge to merge all the .json results in one coverage.json file. Now I want to produce an HTML&text report from that merged .json file.

After trying multiple nyc commands to produce the report from JSON with no luck, I found an interesting behavior of nyc: You have to be in the parent directory of the instrumented code when you are generating a report. For example: If the code I instrumented is in /usr/share/node/**, and the merged coverage.json result is in /tmp directory. If I run nyc report --temp-dir=/tmp --reporter=text under /tmp, I won't get anything.

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

But if I run the same command under /usr/share/node or /, I'm able to get the correct output with coverage numbers. Not sure if it's a weird permission issue in nyc. If it's an expected behavior of nyc, it should be documented. Thanks!

Environment Information

  System:
    OS: Linux 5.3
    CPU: (16) x64 Intel(R) Xeon(R) CPU E5-2686 v4 @ 2.30GHz
    Memory: 64.74 GB / 119.89 GB
  Binaries:
    Node: 12.18.3 - /usr/bin/node
    npm: 6.14.6 - /usr/bin/npm

ansjcy avatar Aug 20 '20 22:08 ansjcy

@ansjcy Thanks for filing this issue as I had the same problem.

My solution was just to replace all the file paths in the coverage-final.json with the current working directory (I had a copy of the code already checked out).

aleccool213 avatar Jul 20 '21 17:07 aleccool213

For anyone running into this, use the --cwd option to set nyc's current working directory to the root of your project.

wesrice avatar Apr 10 '24 02:04 wesrice