istanbuljs / nyc

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

Enabling nyc to generate reports from coverage.raw.json #1249

Open jjangga0214 opened 4 years ago

jjangga0214 commented 4 years ago

Hi, I'd like to make a feature request.

If nyc can generate reports by only reading coverage.json, then it'd be convenient.

coreyfarrell commented 4 years ago

You can tell nyc the directory to find *.json files with coverage: nyc -t coverage_data report. It's important that the directory not contain any *.json files that are not coverage data.

jjangga0214 commented 4 years ago

@coreyfarrell

Thank you for the response!

However, it doesn't generate a report properly, in my test.

I have a directory like this.

nyc-demo
└── coverage-final.json

And I run the command against it.

nyc -t ./nyc-demo report --reporter=html

As expected, this creates coverage directory.

coverage
├── base.css
├── block-navigation.js
├── index.html
├── prettify.css
├── prettify.js
├── sort-arrow-sprite.png
└── sorter.js

But when I open ./coverage/index.html on the browser, an invalid(empty) result is shown, like the picture below.

Screenshot from 2019-12-28 13-41-46

The coverage-final.json is generated by jest out-of-the-box with json reportor (And I just copied it to ./nyc-demo/), and the html report by jest was the below.

Screenshot from 2019-12-28 13-44-00

I'm curious why this happens.

coreyfarrell commented 4 years ago

Without a simple repository demonstrating the issue I cannot help you.

jjangga0214 commented 4 years ago

@coreyfarrell

I found an interesting fact.

I made a very simple repo jjangga0214/nyc-issue-1249-js, using jest and nyc. I tested it with nyc@15.0.0 and nyc@14.1.1 (but the repo's package.json only specifies 15.0.0). I found both ones work well.

After that, I also made jjangga0214/nyc-issue-1249-ts, using typescript, and ts-jest as preset of jest. Other settings are same as nyc-issue-1249-js.

However, this time, I found nyc@15.0.0 works well, but nyc@14.1.1 doesn't (but the repo's package.json and README.md only mentions the result of 15.0.0). When I used nyc@14.1.1 from the repo, I faced an invalid (empty) report (same as a screenshot from the previous comment).

I noticed that there is a little difference between coverage-final.json (raw json coverage generated by jest with its json reporter(check out jest.config.js)) in nyc-issue-1249-js and nyc-issue-1249-ts.

nyc-issue-1249-js/coverage/coverage-final.json looks like this.

{
  "/media/gilchan/SHARE/nyc-issue-1249-js/index.js": {
    "path": "/media/gilchan/SHARE/nyc-issue-1249-js/index.js",
    "statementMap": {
      "0": {
        "start": { "line": 1, "column": 0 },
        "end": { "line": 3, "column": 2 }
      },
      "1": {
        "start": { "line": 2, "column": 2 },
        "end": { "line": 2, "column": 23 }
      }
    },
    "fnMap": {
      "0": {
        "name": "(anonymous_0)",
        "decl": {
          "start": { "line": 1, "column": 17 },
          "end": { "line": 1, "column": 18 }
        },
        "loc": {
          "start": { "line": 1, "column": 23 },
          "end": { "line": 3, "column": 1 }
        },
        "line": 1
      }
    },
    "branchMap": {},
    "s": { "0": 1, "1": 1 },
    "f": { "0": 1 },
    "b": {},
    "_coverageSchema": "43e27e138ebf9cfc5966b082cf9a028302ed4184",
    "hash": "f9d542570b9259c86f6a7d6b0115468e1f952583"
  }
}

But, nyc-issue-1249-ts/coverage/coverage-final.json (not nyc-issue-1249-js, but nyc-issue-1249-ts!) does not have _coverageSchema and hash field (rest fields are identical).

I guess this difference is caused by ts-jest.

So, my conclusion is that nyc@14.1.1 requires _coverageSchema and hash field, while nyc@15.0.0 does not.

coreyfarrell commented 4 years ago

This is difficult to troubleshoot at this time. Current releases of jest still use istanbuljs modules associated with nyc 14, I've seem compatibility issues when trying to mix previous and current istanbuljs modules. I think we're going to need to wait for the next release of jest 25.x which will use the istanbuljs modules associated with nyc 15.

jjangga0214 commented 4 years ago

@coreyfarrell I just read nyc's docs first time a few days ago. To me, it was unclear that nyc can generate reports from coverage.json.

For more reader-friendly docs(and for folks who do not know .nyc_output), how do you think an explicit mention on this feature with practical command usage example (like below)?

# this reads json coverage files from ./dir-containing-coverage-json 
# and generate html report
# to ./dir-for-report
nyc report \
  --temp-dir ./dir-containing-coverage-json \
  --reporter html \
  --report-dir ./dir-for-report
ColCh commented 4 years ago

@jjangga0214 thank you for my saved day mate

ansjcy commented 4 years ago

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.

stale[bot] commented 3 years ago

Is this still relevant? If so, what is blocking it? Is there anything you can do to help move it forward?

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs.

thefurdui commented 2 years ago

@ansjcy, didn't you try to run this report generator even outside of the tested project repo? I got completely stuck at this point