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

test_runner: avoid error when coverage line not found #53000

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions lib/internal/test_runner/coverage.js
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ class TestCoverage {
if (isBlockCoverage) {
ArrayPrototypePush(branchReports, {
__proto__: null,
line: range.lines[0].line,
line: range.lines[0]?.line,
count: range.count,
});

Expand All @@ -197,7 +197,7 @@ class TestCoverage {
__proto__: null,
name: functions[j].functionName,
count: maxCountPerFunction,
line: range.lines[0].line,
line: range.lines[0]?.line,
});

if (range.count !== 0 || range.ignoredLines === range.lines.length) {
Expand Down
11 changes: 11 additions & 0 deletions test/fixtures/test-runner/coverage-loader/hooks.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
const source = `
import { test } from 'node:test';
test('test', async () => {});
`;

export async function load(url, context, nextLoad) {
if (url.endsWith('virtual.js')) {
return { format: "module", source, shortCircuit: true };
}
return nextLoad(url, context);
}
4 changes: 4 additions & 0 deletions test/fixtures/test-runner/coverage-loader/register-hooks.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
const { register } = require('node:module');
const { pathToFileURL } = require('node:url');

register('./hooks.mjs', pathToFileURL(__filename));
Empty file.
30 changes: 30 additions & 0 deletions test/parallel/test-runner-coverage.js
Original file line number Diff line number Diff line change
Expand Up @@ -273,3 +273,33 @@ test('coverage with source maps', skipIfNoInspector, () => {
assert(result.stdout.toString().includes(report));
assert.strictEqual(result.status, 1);
});

test('coverage with ESM hook - source irrelevant', skipIfNoInspector, () => {
let report = [
'# start of coverage report',
'# ------------------------------------------------------------------',
'# file | line % | branch % | funcs % | uncovered lines',
'# ------------------------------------------------------------------',
'# hooks.mjs | 100.00 | 100.00 | 100.00 | ',
'# register-hooks.js | 100.00 | 100.00 | 100.00 | ',
'# virtual.js | 100.00 | 100.00 | 100.00 | ',
'# ------------------------------------------------------------------',
'# all files | 100.00 | 100.00 | 100.00 |',
'# ------------------------------------------------------------------',
'# end of coverage report',
].join('\n');

if (common.isWindows) {
report = report.replaceAll('/', '\\');
}

const fixture = fixtures.path('test-runner', 'coverage-loader');
const args = [
'--import', './register-hooks.js', '--test', '--experimental-test-coverage', '--test-reporter', 'tap', 'virtual.js',
];
const result = spawnSync(process.execPath, args, { cwd: fixture });

assert.strictEqual(result.stderr.toString(), '');
assert(result.stdout.toString().includes(report));
assert.strictEqual(result.status, 0);
});
Loading