c8 icon indicating copy to clipboard operation
c8 copied to clipboard

missing coverage of closing brace in switch/case when const is declared

Open tbiesemann opened this issue 2 years ago • 4 comments

  • Node Version:18.14.0
  • c8 Version:8.0.1
  • Platform:Darwin C02C26P7MD6T 23.0.0 Darwin Kernel Version 23.0.0: Fri Sep 15 14:42:42 PDT 2023; root:xnu-10002.1.13~1/RELEASE_X86_64 x86_64
function huhu(someValue) {
	switch (someValue) {
		case true:
			const result = 42;
			return result;
		default:
			return 43;
	}
};
huhu(true);
huhu(false);

Execute with

npx c8 node huhu.js

Reported coverage: image

If i omit the const declaration and just return 42; the coverage is 100%. Expectation is to have 100% coverage for the above snippet.

tbiesemann avatar Oct 30 '23 09:10 tbiesemann

I have the exact same issue covering this function:

https://gitlab.com/eric.morand/twing/-/blob/one-hundred/src/lib/parser.ts?ref_type=heads#L348

ericmorand avatar Nov 17 '23 15:11 ericmorand

@tbiesemann , in case you are still struggling with the issue, know that if you actually scope your cases body, the issue disappears:

function huhu(someValue) {
	switch (someValue) {
		case true: {
			const result = 42;
			return result;
                }
		default: {
			return 43;
                }
	}
};
huhu(true);
huhu(false);

I still don't have an explanation why, but, with your original file, the coverage data emitted by V8 actually include an uncovered range at 144:145 - that is just after the } at line 8 and spans up to the } at line 9. So c8 can't actually do much about that.

This is probably worth raising an issue at v8 repository.

ericmorand avatar Jun 28 '24 23:06 ericmorand

@tbiesemann , @bcoe , I opened an issue in node.js issue tracker. This is likely to be a v8 issue but since I'm using node.js to reproduce the issue, I have to start there.

ericmorand avatar Jun 30 '24 13:06 ericmorand

There's another open c8 issue #229 asking about an uncovered line of a finally block. I am not sure whether these are related to the same underlying v8 issue but perhaps the repro linked in the comment can help with troubleshooting.

That other issue's coverage output looks eerily similar:

archiehharris avatar Jul 02 '24 14:07 archiehharris