Skip to content

Commit e2211a0

Browse files
MoLowtargos
authored andcommitted
test_runner: avoid error when coverage line not found
PR-URL: #53000Fixes: #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 879679e commit e2211a0

5 files changed

Lines changed: 47 additions & 2 deletions

File tree

‎lib/internal/test_runner/coverage.js‎

Lines changed: 2 additions & 2 deletions
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){
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
constsource=`
2+
import { test } from 'node:test';
3+
test('test', async () => {});
4+
`;
5+
6+
exportasyncfunctionload(url,context,nextLoad){
7+
if(url.endsWith('virtual.js')){
8+
return{format: "module", source,shortCircuit: true};
9+
}
10+
returnnextLoad(url,context);
11+
}
Lines changed: 4 additions & 0 deletions
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‎

Lines changed: 30 additions & 0 deletions
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+
letreport=[
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+
constfixture=fixtures.path('test-runner','coverage-loader');
297+
constargs=[
298+
'--import','./register-hooks.js','--test','--experimental-test-coverage','--test-reporter','tap','virtual.js',
299+
];
300+
constresult=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)