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

--experimental-test-coverage throws error: "TypeError: Cannot read properties of undefined (reading 'line')" #51552

Closed
nicusorflorin opened this issue Jan 23, 2024 · 3 comments
Labels
test_runner Issues and PRs related to the test runner subsystem.

Comments

@nicusorflorin
Copy link

Version

v20.11.0

Platform

Linux MyPC 5.15.133.1-microsoft-standard-WSL2 #1 SMP Thu Oct 5 21:02:42 UTC 2023 x86_64 x86_64 x86_64 GNU/Linux

Subsystem

No response

What steps will reproduce the bug?

  1. Create a few basic tests
  2. Run them with the command NODE_V8_COVERAGE=./coverage node --test --test-reporter=junit --test-reporter-destination=results.xml --experimental-test-coverage -r ts-node/register test/**/*.spec.ts

How often does it reproduce? Is there a required condition?

100%

What is the expected behavior? Why is that the expected behavior?

Should generate proper coverage

What do you see instead?

content of results.xml:

<?xml version="1.0" encoding="utf-8"?>
<testsuites>
	<testsuite name="TestSuite1Name" time="0.016595" disabled="0" errors="0" tests="3" failures="0" skipped="0" hostname="MyPC">
		<testcase name="test name 1" time="0.007770" classname="test"/>
		<testcase name="test name 2" time="0.002685" classname="test"/>
		<testcase name="test name 3" time="0.001173" classname="test"/>
	</testsuite>
	<testsuite name="TestSuite2Name" time="0.013062" disabled="0" errors="0" tests="1" failures="0" skipped="0" hostname="MyPC">
		<testcase name="test name 1" time="0.007652" classname="test"/>
	</testsuite>
	<!-- Warning: Could not report code coverage. TypeError: Cannot read properties of undefined (reading 'line') -->
	<!-- tests 4 -->
	<!-- suites 2 -->
	<!-- pass 4 -->
	<!-- fail 0 -->
	<!-- cancelled 0 -->
	<!-- skipped 0 -->
	<!-- todo 0 -->
	<!-- duration_ms 4851.507124 -->
</testsuites>

Additional information

No response

@MoLow
Copy link
Member

MoLow commented Jan 23, 2024

Unfortunately, this is a known limitation, and source maps are not currently supported: https://nodejs.org/api/test.html#limitations
PR's fixing this are more than welcome

@MoLow MoLow closed this as not planned Won't fix, can't repro, duplicate, stale Jan 23, 2024
@MoLow
Copy link
Member

MoLow commented Jan 23, 2024

closed as duplicate of #51392

@MoLow MoLow added the test_runner Issues and PRs related to the test runner subsystem. label Jan 23, 2024
@ddurschlag
Copy link

ddurschlag commented Apr 12, 2024

@MoLow Are you sure this is a sourcemap issue? I've run into the same thing, and it's fixed by putting an arbitrary import at the top of the file. It seems like the test runner (or coverage collector?) is failing in some way when a module has exports but no imports. Sourcemapping doesn't seem to be involved.

Edit: I've investigated further, and this seems to be file-length related. Adding meaningless comments to the file can make it work/not work. In my testing, I hit upon a 113/114 border: if the file is 113 bytes or fewer, it appears to fail, while 114 bytes or greater seems to succeed. I can make coverage succeed/fail by removing/re-adding a leading newline in the file. Here is a 114 byte file that coverage works for, but removing any single byte will cause it to fail in my testing:

export function sayHello () {
  console.log('Hello, Console!');
}
// These chars are here to get to 114 bytesxxxxx

Note there can be no trailing newline and this will work. Removing an x (or any other character) will make it fail.

Replacing one of the final xes with a trailing newline oddly doesn't work -- but replacing it with 2 newlines does, making the minimum size 115 bytes at that point. I do not know what influences the byte-floor.

Node version is v20.10.0.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
test_runner Issues and PRs related to the test runner subsystem.
Projects
None yet
Development

No branches or pull requests

3 participants