Skip to content

Commit 2b86de9

Browse files
committed
test_runner: require --enable-source-maps for source map coverage
1 parent 3e7eb05 commit 2b86de9

File tree

3 files changed

+43
-5
lines changed

3 files changed

+43
-5
lines changed

lib/internal/test_runner/coverage.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -338,7 +338,7 @@ class TestCoverage {
338338
mapCoverageWithSourceMap(coverage) {
339339
const { result } = coverage;
340340
const sourceMapCache = coverage['source-map-cache'];
341-
if (!sourceMapCache) {
341+
if (!this.globalOptions.sourceMaps || !sourceMapCache) {
342342
return result;
343343
}
344344
const newResult = new SafeMap();

lib/internal/test_runner/test.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -552,7 +552,8 @@ class Test extends AsyncResource {
552552
file: loc[2],
553553
};
554554

555-
if (this.config.sourceMaps === true) {
555+
// TODO(RedYetiDev): This should be supported with code coverage
556+
if (this.config.sourceMaps && !this.config.coverage) {
556557
const map = lazyFindSourceMap(this.loc.file);
557558
const entry = map?.findEntry(this.loc.line - 1, this.loc.column - 1);
558559

@@ -563,7 +564,7 @@ class Test extends AsyncResource {
563564
}
564565
}
565566

566-
if (StringPrototypeStartsWith(this.loc.file, 'file://')) {
567+
if (this.loc.file && StringPrototypeStartsWith(this.loc.file, 'file://')) {
567568
this.loc.file = fileURLToPath(this.loc.file);
568569
}
569570
}

test/parallel/test-runner-coverage.js

Lines changed: 39 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -289,6 +289,37 @@ test('coverage reports on lines, functions, and branches', skipIfNoInspector, as
289289
});
290290
});
291291

292+
test('coverage without --enable-source-map ignores sourcemaps', skipIfNoInspector, () => {
293+
let report = [
294+
'# start of coverage report',
295+
'# --------------------------------------------------------------',
296+
'# file | line % | branch % | funcs % | uncovered lines',
297+
'# --------------------------------------------------------------',
298+
'# a.test.mjs | 100.00 | 100.00 | 100.00 | ',
299+
'# index.test.js | 71.43 | 66.67 | 100.00 | 6-7',
300+
'# stdin.test.js | 100.00 | 100.00 | 100.00 | ',
301+
'# --------------------------------------------------------------',
302+
'# all files | 85.71 | 87.50 | 100.00 | ',
303+
'# --------------------------------------------------------------',
304+
'# end of coverage report',
305+
].join('\n');
306+
307+
if (common.isWindows) {
308+
report = report.replaceAll('/', '\\');
309+
}
310+
311+
const fixture = fixtures.path('test-runner', 'coverage');
312+
const args = [
313+
'--test',
314+
'--experimental-test-coverage',
315+
'--test-reporter', 'tap',
316+
];
317+
const result = spawnSync(process.execPath, args, { cwd: fixture });
318+
assert.strictEqual(result.stderr.toString(), '');
319+
assert(result.stdout.toString().includes(report));
320+
assert.strictEqual(result.status, 1);
321+
});
322+
292323
test('coverage with source maps', skipIfNoInspector, () => {
293324
let report = [
294325
'# start of coverage report',
@@ -311,7 +342,10 @@ test('coverage with source maps', skipIfNoInspector, () => {
311342

312343
const fixture = fixtures.path('test-runner', 'coverage');
313344
const args = [
314-
'--test', '--experimental-test-coverage', '--test-reporter', 'tap',
345+
'--enable-source-maps',
346+
'--test',
347+
'--experimental-test-coverage',
348+
'--test-reporter', 'tap',
315349
];
316350
const result = spawnSync(process.execPath, args, { cwd: fixture });
317351

@@ -489,7 +523,10 @@ test('coverage with included and excluded files', skipIfNoInspector, () => {
489523
test('properly accounts for line endings in source maps', skipIfNoInspector, () => {
490524
const fixture = fixtures.path('test-runner', 'source-map-line-lengths', 'index.js');
491525
const args = [
492-
'--test', '--experimental-test-coverage', '--test-reporter', 'tap',
526+
'--enable-source-maps',
527+
'--test',
528+
'--experimental-test-coverage',
529+
'--test-reporter', 'tap',
493530
fixture,
494531
];
495532
const report = [

0 commit comments

Comments
 (0)