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

3

10

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
Gospel answered 1/5, 2020 at 17:11 Comment(0)
G
20

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).

Gospel answered 6/5, 2020 at 19:38 Comment(0)
E
7

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

Expiate answered 20/8, 2020 at 22:27 Comment(2)
Yu can also do it like this: ``` npx nyc report --reporter=text-summary --cwd <pah to parent of src> -t <path to .nyc_output> ```Ridglea
Incredible, thanks for this. This was it for me, too.Illampu
K
2

In my case, I had to pass --exclude-after-remap=false to generate a report from Typescript integration tests and unit tests coverage json files:

npx nyc report --reporter html --reporter text -t coverage --report-dir coverage/summary --exclude-after-remap false
Kummerbund answered 6/4, 2023 at 4:41 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.