Skip to content

Commit d4442a9

Browse files
authored
test_runner: avoid error when coverage line not found
PR-URL: #53000 Fixes: #52775 Reviewed-By: Chemi Atlow <chemi@atlow.co.il> Reviewed-By: Colin Ihrig <cjihrig@gmail.com> Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com> Reviewed-By: Yagiz Nizipli <yagiz.nizipli@sentry.io>
1 parent fac55e3 commit d4442a9

File tree

5 files changed

+47
-2
lines changed

5 files changed

+47
-2
lines changed

lib/internal/test_runner/coverage.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,7 @@ class TestCoverage {
177177
if (isBlockCoverage) {
178178
ArrayPrototypePush(branchReports, {
179179
__proto__: null,
180-
line: range.lines[0].line,
180+
line: range.lines[0]?.line,
181181
count: range.count,
182182
});
183183

@@ -197,7 +197,7 @@ class TestCoverage {
197197
__proto__: null,
198198
name: functions[j].functionName,
199199
count: maxCountPerFunction,
200-
line: range.lines[0].line,
200+
line: range.lines[0]?.line,
201201
});
202202

203203
if (range.count !== 0 || range.ignoredLines === range.lines.length) {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
const source = `
2+
import { test } from 'node:test';
3+
test('test', async () => {});
4+
`;
5+
6+
export async function load(url, context, nextLoad) {
7+
if (url.endsWith('virtual.js')) {
8+
return { format: "module", source, shortCircuit: true };
9+
}
10+
return nextLoad(url, context);
11+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
const { register } = require('node:module');
2+
const { pathToFileURL } = require('node:url');
3+
4+
register('./hooks.mjs', pathToFileURL(__filename));

test/fixtures/test-runner/coverage-loader/virtual.js

Whitespace-only changes.

test/parallel/test-runner-coverage.js

+30
Original file line numberDiff line numberDiff line change
@@ -273,3 +273,33 @@ test('coverage with source maps', skipIfNoInspector, () => {
273273
assert(result.stdout.toString().includes(report));
274274
assert.strictEqual(result.status, 1);
275275
});
276+
277+
test('coverage with ESM hook - source irrelevant', skipIfNoInspector, () => {
278+
let report = [
279+
'# start of coverage report',
280+
'# ------------------------------------------------------------------',
281+
'# file | line % | branch % | funcs % | uncovered lines',
282+
'# ------------------------------------------------------------------',
283+
'# hooks.mjs | 100.00 | 100.00 | 100.00 | ',
284+
'# register-hooks.js | 100.00 | 100.00 | 100.00 | ',
285+
'# virtual.js | 100.00 | 100.00 | 100.00 | ',
286+
'# ------------------------------------------------------------------',
287+
'# all files | 100.00 | 100.00 | 100.00 |',
288+
'# ------------------------------------------------------------------',
289+
'# end of coverage report',
290+
].join('\n');
291+
292+
if (common.isWindows) {
293+
report = report.replaceAll('/', '\\');
294+
}
295+
296+
const fixture = fixtures.path('test-runner', 'coverage-loader');
297+
const args = [
298+
'--import', './register-hooks.js', '--test', '--experimental-test-coverage', '--test-reporter', 'tap', 'virtual.js',
299+
];
300+
const result = spawnSync(process.execPath, args, { cwd: fixture });
301+
302+
assert.strictEqual(result.stderr.toString(), '');
303+
assert(result.stdout.toString().includes(report));
304+
assert.strictEqual(result.status, 0);
305+
});

0 commit comments

Comments
 (0)