Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Incorrect coverage reporting with throw in a block #62

Closed
Trott opened this issue Feb 5, 2019 · 8 comments
Closed

Incorrect coverage reporting with throw in a block #62

Trott opened this issue Feb 5, 2019 · 8 comments

Comments

@Trott
Copy link
Contributor

Trott commented Feb 5, 2019

  • Version: 11.9.0
  • Platform: Darwin LIB-0F7FVH8-LT 18.2.0 Darwin Kernel Version 18.2.0: Mon Nov 12 20:24:46 PST 2018; root:xnu-4903.231.4~2/RELEASE_X86_64 x86_64

In file foo.js:

if (true) {
  throw new Error('foo');
}

I run this:

c8 node foo.js 

I get this output:

/Users/trott/io.js/foo.js:2
  throw new Error('foo');
  ^

Error: foo
    at Object.<anonymous> (/Users/trott/io.js/foo.js:2:9)
    at Module._compile (internal/modules/cjs/loader.js:734:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:745:10)
    at Module.load (internal/modules/cjs/loader.js:626:32)
    at tryModuleLoad (internal/modules/cjs/loader.js:566:12)
    at Function.Module._load (internal/modules/cjs/loader.js:558:3)
    at Function.Module.runMain (internal/modules/cjs/loader.js:797:12)
    at executeUserCode (internal/bootstrap/node.js:526:15)
    at startMainThreadExecution (internal/bootstrap/node.js:439:3)
----------|----------|----------|----------|----------|-------------------|
File      |  % Stmts | % Branch |  % Funcs |  % Lines | Uncovered Line #s |
----------|----------|----------|----------|----------|-------------------|
All files |       50 |    66.67 |      100 |       50 |                   |
 foo.js   |       50 |    66.67 |      100 |       50 |               3,4 |
----------|----------|----------|----------|----------|-------------------|

What I expected:

I would not expect there to be a line 4 mentioned in the coverage report because the script is three lines long.
I would not expect line 3 to be mentioned as uncovered because there is nothing executable on it.
I would expect Lines to be covered 100% (but not Branch).

@thealjey
Copy link

thealjey commented Feb 7, 2019

@Trott if you don't handle errors, how do you expect accurate results? the script terminates after that throw

@Trott
Copy link
Contributor Author

Trott commented Feb 7, 2019

@Trott if you don't handle errors, how do you expect accurate results?

Failing to catch the error is not the problem. I did that for simplicity. Here:

$ cat foo.js
function doSomething(foo) {
  if (foo === 'throw') {
    throw new Error('foo');
  }
  console.log('Your foo is OK.');
}

try {
  doSomething('foo');
  doSomething('throw');
} catch (e) {
  console.log(`caught ${e}`);
}
$ c8 node foo.js
Your foo is OK.
caught Error: foo
----------|----------|----------|----------|----------|-------------------|
File      |  % Stmts | % Branch |  % Funcs |  % Lines | Uncovered Line #s |
----------|----------|----------|----------|----------|-------------------|
All files |    92.86 |       80 |      100 |    92.86 |                   |
 foo.js   |    92.86 |       80 |      100 |    92.86 |                 4 |
----------|----------|----------|----------|----------|-------------------|
$ 

It is still incorrectly telling me that there is an uncovered branch when there is not. It is also still incorrectly reporting that line 4 is uncovered. Line 4 does not contain any executable code. Both branches of the if statement are being exercised. All errors are being caught.

@Trott
Copy link
Contributor Author

Trott commented Feb 7, 2019

Incidentally, the throw isn't the problem per se. It appears that anything that returns control from the block will result in mis-reporting that the brace on a line by itself is uncovered.

$ cat foo.js
function doSomething(foo) {
  if (foo === 'return') {
    return foo;
  }
  console.log('Your foo is OK.');
}

doSomething('foo');
doSomething('return');
$ c8 node foo.js
Your foo is OK.
----------|----------|----------|----------|----------|-------------------|
File      |  % Stmts | % Branch |  % Funcs |  % Lines | Uncovered Line #s |
----------|----------|----------|----------|----------|-------------------|
All files |       90 |       80 |      100 |       90 |                   |
 foo.js   |       90 |       80 |      100 |       90 |                 4 |
----------|----------|----------|----------|----------|-------------------|
$ 

@thealjey
Copy link

thealjey commented Feb 8, 2019

I think this is a duplicate of #54

@shinnn
Copy link
Contributor

shinnn commented Apr 8, 2019

Though the V8 bug referenced in #62 (comment) still seems unresolved, with Node.js v11.13.0 the code and command in #62 (comment) works as expected now.

$ node -v
v11.13.0

$ npx c8 node foo.js
/Users/Shinnosuke/foo.js:2
  throw new Error('foo');
  ^

Error: foo
    at Object.<anonymous> (/Users/Shinnosuke/foo.js:2:9)
    at Module._compile (internal/modules/cjs/loader.js:805:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:816:10)
    at Module.load (internal/modules/cjs/loader.js:672:32)
    at tryModuleLoad (internal/modules/cjs/loader.js:612:12)
    at Function.Module._load (internal/modules/cjs/loader.js:604:3)
    at Function.Module.runMain (internal/modules/cjs/loader.js:868:12)
    at internal/main/run_main_module.js:21:11
----------|----------|----------|----------|----------|-------------------|
File      |  % Stmts | % Branch |  % Funcs |  % Lines | Uncovered Line #s |
----------|----------|----------|----------|----------|-------------------|
All files |      100 |      100 |      100 |      100 |                   |
 foo.js   |      100 |      100 |      100 |      100 |                   |
----------|----------|----------|----------|----------|-------------------|

@Trott
Copy link
Contributor Author

Trott commented Apr 9, 2019

Though the V8 bug referenced in #62 (comment) still seems unresolved, with Node.js v11.13.0 the code and command in #62 (comment) works as expected now.

I believe @bcoe floated a fix in Node.js itself for the V8 that ships with Node.js.

@bcoe
Copy link
Owner

bcoe commented Oct 24, 2019

this issue is corrected as of Node 13.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants