Incorrect branch coverage for async methods
See original GitHub issue- v12.22.5:
- macOS 11.5.2:
I’ve noticed some inexplicable uncovered branch reports after switching from nyc
to c8
. The uncovered branches were the closing braces of async methods. The issue can be reproduced with the following code:
class Test {
constructor(msg) {
this.msg = msg;
}
async say() {
return this.msg;
} // uncovered branch?
}
const main = async () => {
const t = new Test('hello');
console.log(await t.say());
};
main().catch(console.error);
Running c8:
npx c8 node src/test.js
hello
----------|---------|----------|---------|---------|-------------------
File | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s
----------|---------|----------|---------|---------|-------------------
All files | 100 | 80 | 100 | 100 |
test.js | 100 | 80 | 100 | 100 | 8
----------|---------|----------|---------|---------|-------------------
Line 8 is the closing }
of the async test()
method.
Issue Analytics
- State:
- Created 2 years ago
- Reactions:1
- Comments:6 (4 by maintainers)
Top Results From Across the Web
Incorrect branch coverage with async/await test #623 - GitHub
The build script loops through all Test projects and creates coverageresult files and then those are given to reportgenerator. Seems that code ...
Read more >Code Coverage does not show async methods
I'm using Visual Studio 2017 v15.6.1 with c# projects and mstest unit tests. My tests are async and use async methods. When running...
Read more >Code coverage for async methods - Stack Overflow
This basically forces the state machine into the async path, providing the complete coverage. To be sure, this is not a perfect solution....
Read more >Incorrectly calculate coverage when DisposeAsync is involved
DisposeAsync(); It seems, SonarQube calculates conditions coverage incorrectly. Even if all lines are covered with tests, SonarQube marks the ...
Read more >Code Coverage with Async Await - Presentation Source
The reason the code is not shown as being covered has to do with how async methods are implemented. The C# compiler actually...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
@stefan-guggisberg perhaps keep this issue open, so other folks understand this category of issue.
@stefan-guggisberg this was likely a bug in v8 itself, which appears to have been fixed in newer versions of Node.js (I couldn’t reproduce in v14/v16)
Unfortunately these updates to v8 can’t always be back-ported to older Node.js versions (so I won’t be able to fix in c8).
Can you potentially run coverage on a newer version of Node.js? while still testing on Node 12, i.e., run coverage on the latest and greatest Node.js, test on all versions you support.