问题
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
回答1:
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).
回答2:
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
来源:https://stackoverflow.com/questions/61547209/how-to-generate-nyc-report-from-json-results-no-nyc-output