bcoe / c8

output coverage reports using Node.js' built in coverage
ISC License
1.99k stars 90 forks source link

Lines without statements (empty lines) on them are counted as statements #500

Open millerick opened 11 months ago

millerick commented 11 months ago

I put together a minimal example in https://github.com/millerick/c8-ts-line-statement-example

The coverage report for it is also committed to that repository, but here is a screenshot of it: image

Here I do not expect line 4 to be counted as a statement. I also would not expect lines 3 or 7 to be counted as statements, but am less opinionated about that.

I read through c8's documentation and did not find any configuration settings I could change to produce the behavior I expected. Is there something that I am missing? Is there a way to improve this so that only lines with actual code statements on them are counted as statements?

millerick commented 10 months ago

@bcoe , can you comment on this and whether or not it is possible to change this behavior?

bcoe commented 9 months ago

@millerick I'm not quite sure what my reasoning was for the logic here:

https://github.com/istanbuljs/v8-to-istanbul/blob/b0eb41056feff1863d27a1952cf2441e59e4daa7/lib/v8-to-istanbul.js#L278

I believe a line object is added for each line (correctly) but we also iterate over all these lines turning them into statements. It may be that, if a line has no statements on it, we could skip it.

millerick commented 9 months ago

It may be that, if a line has no statements on it, we could skip it.

Meaning do not create statements for lines that do not actually have a statement on them?

millerick commented 9 months ago

I spent some time trying to look at this, and while it is easy to get line 4 to count as neither a statement nor a line, I can't find a way to get it to count as a line but not a statement. I tried looking through other code bases I have that are still using nyc for coverage, and I found examples where the number of lines is less than the number of statements, which is not what I would have expected.

I do think that having empty lines not counted as either lines or statements is more correct than the current behavior. @bcoe , if you agree, I will submit a pull request for that change. However, I am unsure if it is possible to make the total number of lines/statements differ from each other in c8 (which may just be an expected difference with c8 when compared to nyc).

Quramy commented 7 months ago

I am encountering the same problem. I use c8 and jest to measure coverage and upload them to Codecov. The number of total lines measured by c8 is more than that measured by jest. This causes the line coverage measured by jest to be under-represented than it should be. (here is my project Codecov report)

Is there a way or setting to combine the coverage measured by jest (or any other tool using Istanbul instrumentation) with the coverage measured by c8?

Quramy commented 7 months ago

Is there a way or setting to combine the coverage measured by jest (or any other tool using Istanbul instrumentation) with the coverage measured by c8?

For now, my problem is resolved configuring https://jestjs.io/ja/docs/configuration#coverageprovider-string to "v8" .