Search code examples
istanbulnyc

How to generate nyc report from json results (no .nyc_output)?


I've inherited a JS code base with Jasmine unit tests. The testing framework uses karma and instanbul-combine to get code coverage. It seems istanbul-combine isn't working with present node modules, and besides is no longer maintained: the recommended replacement is nyc. I'm having trouble replacing istanbul-combine with nyc in the Makefile.

I succeeded in merging my separate coverage results (json) files into a single coverage-final.json file (this SO question), but now I need to generate the summary report.

How do I generate a summary report from a coverage.json file?

One problem here, I think, is that I have no .nyc_output directory with intermediate results, since I'm not using nyc to generate coverage data. All my coverage data is in a coverage directory and its child directories.

I've tried specifying a filename:

npx nyc report --include coverage-final.json

Also tried specifying the directory:

npx nyc report --include coverage

Neither works.

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

The CLI help documentation says

  --temp-dir, -t          directory to read raw coverage information from

But when I use that point to coverage directory (viz., npx nyc report -t coverage), I get the same unsatisfactory result. NYC is apparently fairly rigid in the formats it will accept this data.


Here's the original Makefile line that I'm replacing:

PATH=$(PROJECT_HOME)/bin:$$PATH node_modules/istanbul-combine/cli.js \
    -d coverage/summary -r html \
    coverage/*/coverage-final.json

Solution

  • Using this line in my Makefile worked:

    npx nyc report --reporter html --reporter text -t coverage --report-dir coverage/summary
    

    It grabs the JSON files from the coverage directory and puts them altogether into an HTML report in the coverage/summary subdirectory. (Didn't need the nyc merge command from my previous question/answer.)

    I'm not sure why the -t option didn't work before. It may be I was using the wrong version of nyc (15.0.0 instead of 14.1.1, fwiw).