Skip to content

Commit 025f1a9

Browse files
committed
test: test case when --enable-source-maps is provided
1 parent 2e2ab53 commit 025f1a9

File tree

2 files changed

+83
-2
lines changed

2 files changed

+83
-2
lines changed

lib/internal/test_runner/test.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ const {
1414
Number,
1515
NumberPrototypeToFixed,
1616
ObjectDefineProperty,
17-
ObjectKeys,
1817
ObjectSeal,
1918
Promise,
2019
PromisePrototypeThen,
@@ -561,7 +560,7 @@ class Test extends AsyncResource {
561560
const map = lazyFindSourceMap(this.loc.file);
562561
const entry = map?.findEntry(this.loc.line - 1, this.loc.column - 1);
563562

564-
if (entry !== undefined && ObjectKeys(entry).length > 0) {
563+
if (entry?.originalSource !== undefined) {
565564
this.loc.line = entry.originalLine + 1;
566565
this.loc.column = entry.originalColumn + 1;
567566
this.loc.file = entry.originalSource;

test/parallel/test-runner-coverage.js

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

292+
test('coverage with source maps', skipIfNoInspector, () => {
293+
let report = [
294+
'# start of coverage report',
295+
'# --------------------------------------------------------------',
296+
'# file | line % | branch % | funcs % | uncovered lines',
297+
'# --------------------------------------------------------------',
298+
'# a.test.ts | 53.85 | 100.00 | 100.00 | 8-13', // part of a bundle
299+
'# b.test.ts | 55.56 | 100.00 | 100.00 | 1 7-9', // part of a bundle
300+
'# index.test.js | 71.43 | 66.67 | 100.00 | 6-7', // no source map
301+
'# stdin.test.ts | 57.14 | 100.00 | 100.00 | 4-6', // Source map without original file
302+
'# --------------------------------------------------------------',
303+
'# all files | 58.33 | 87.50 | 100.00 | ',
304+
'# --------------------------------------------------------------',
305+
'# end of coverage report',
306+
].join('\n');
307+
308+
if (common.isWindows) {
309+
report = report.replaceAll('/', '\\');
310+
}
311+
312+
const fixture = fixtures.path('test-runner', 'coverage');
313+
const args = [
314+
'--test', '--experimental-test-coverage', '--test-reporter', 'tap',
315+
];
316+
const result = spawnSync(process.execPath, args, { cwd: fixture });
317+
318+
assert.strictEqual(result.stderr.toString(), '');
319+
assert(result.stdout.toString().includes(report));
320+
assert.strictEqual(result.status, 1);
321+
});
322+
323+
324+
test('coverage with --enable-source-maps option', skipIfNoInspector, () => {
325+
const fixture = fixtures.path('test-runner', 'coverage', 'stdin.test.js');
326+
const args = [
327+
'--test', '--experimental-test-coverage', '--enable-source-maps', '--test-reporter', 'tap',
328+
fixture,
329+
];
330+
331+
let report = [
332+
'# start of coverage report',
333+
'# ------------------------------------------------------------------',
334+
'# file | line % | branch % | funcs % | uncovered lines',
335+
'# ------------------------------------------------------------------',
336+
'# test | | | | ',
337+
'# fixtures | | | | ',
338+
'# test-runner | | | | ',
339+
'# coverage | | | | ',
340+
'# stdin.test.ts | 57.14 | 100.00 | 100.00 | 4-6',
341+
'# ------------------------------------------------------------------',
342+
'# all files | 57.14 | 100.00 | 100.00 | ',
343+
'# ------------------------------------------------------------------',
344+
'# end of coverage report',
345+
].join('\n');
346+
347+
if (common.isWindows) {
348+
report = report.replaceAll('/', '\\');
349+
}
350+
351+
const result = spawnSync(process.execPath, args);
352+
const stdOut = result.stdout.toString();
353+
354+
assert.strictEqual(result.stderr.toString(), '');
355+
assert(!stdOut.includes('TypeError'));
356+
assert(stdOut.includes(report));
357+
assert.strictEqual(result.status, 0);
358+
});
359+
360+
test('coverage with source maps missing sources', skipIfNoInspector, () => {
361+
const file = fixtures.path('test-runner', 'source-map-missing-sources', 'index.js');
362+
const missing = fixtures.path('test-runner', 'source-map-missing-sources', 'nonexistent.js');
363+
const result = spawnSync(process.execPath, [
364+
'--test',
365+
'--experimental-test-coverage',
366+
file,
367+
]);
368+
369+
const error = `Cannot find '${pathToFileURL(missing)}' imported from the source map for '${pathToFileURL(file)}'`;
370+
assert(result.stdout.toString().includes(error));
371+
assert.strictEqual(result.status, 1);
372+
});
373+
292374
test('coverage with ESM hook - source irrelevant', skipIfNoInspector, () => {
293375
let report = [
294376
'# start of coverage report',

0 commit comments

Comments
 (0)