istanbuljs / nyc

the Istanbul command line interface
https://istanbul.js.org/
ISC License
5.55k stars 353 forks source link

NYC Producing NaN coverage if relative paths change #1493

Open PalAditya opened 1 year ago

PalAditya commented 1 year ago

Scenario: I have a gitlab job where I am running my tests using jest, and moving all the coverage-final.json files into another directory.
Now, I am using a second gitlab job which downloads the coverage-xyz.json files (As artifacts), merges them with nyc merge and then then tries to output the report with cobertura/html (npx nyc report -t ..) - which produces NaN coverage. I am suspecting that the culprit is the path change that happens when we switch between gitlab jobs, since if I download the artifacts and run the commands, coverage is still NaN, but on manually correcting the paths to my local setup, the output is correct.

Question: Is it expected that nyc report will fail to give proper cobertura/html output if the paths reported in coverage.json aren't valid? If yes, are there any options to set them to relative and not absolute paths?

kbrooks commented 1 year ago

You can automatically substitute the paths in your runner script like this:

find final-coverage-files -type f -exec sed -i 's_/absolute/path/inside/your/ci/runner'$(pwd)'_g' {} +