question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

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:open
  • Created 2 years ago
  • Reactions:1
  • Comments:6 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
bcoecommented, Sep 9, 2021

@stefan-guggisberg perhaps keep this issue open, so other folks understand this category of issue.

1reaction
bcoecommented, Sep 7, 2021

@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.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found