Skip to content

Commit

Permalink
test_runner: avoid error when coverage line not found
Browse files Browse the repository at this point in the history
PR-URL: nodejs#53000
Fixes: nodejs#52775
Reviewed-By: Chemi Atlow <[email protected]>
Reviewed-By: Colin Ihrig <[email protected]>
Reviewed-By: Benjamin Gruenbaum <[email protected]>
Reviewed-By: Yagiz Nizipli <[email protected]>
  • Loading branch information
MoLow authored May 17, 2024
1 parent fac55e3 commit d4442a9
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 2 deletions.
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);
});

0 comments on commit d4442a9

Please sign in to comment.