Skip to content

Commit 4badc52

Browse files
avivkellercjihrigjaydenseric
committed
test_runner: report error on missing sourcemap source
Fixes #54756 Co-Authored-By: Colin Ihrig <cjihrig@gmail.com> Co-Authored-By: Jayden Seric <me@jaydenseric.com>
1 parent 8b8fc53 commit 4badc52

File tree

9 files changed

+46
-12
lines changed

9 files changed

+46
-12
lines changed

doc/api/errors.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2573,6 +2573,12 @@ disconnected socket.
25732573

25742574
A call was made and the UDP subsystem was not running.
25752575

2576+
<a id="ERR_SOURCE_MAP_MISSING_SOURCE"></a>
2577+
2578+
### `ERR_SOURCE_MAP_MISSING_SOURCE`
2579+
2580+
A file imported from a source map was not found.
2581+
25762582
<a id="ERR_SQLITE_ERROR"></a>
25772583

25782584
### `ERR_SQLITE_ERROR`

lib/internal/errors.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1704,6 +1704,7 @@ E('ERR_SOCKET_CONNECTION_TIMEOUT',
17041704
E('ERR_SOCKET_DGRAM_IS_CONNECTED', 'Already connected', Error);
17051705
E('ERR_SOCKET_DGRAM_NOT_CONNECTED', 'Not connected', Error);
17061706
E('ERR_SOCKET_DGRAM_NOT_RUNNING', 'Not running', Error);
1707+
E('ERR_SOURCE_MAP_MISSING_SOURCE', `Cannot find '%s' imported from the source map for '%s'`, Error);
17071708
E('ERR_SRI_PARSE',
17081709
'Subresource Integrity string %j had an unexpected %j at position %d',
17091710
SyntaxError);

lib/internal/test_runner/coverage.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,11 @@ const { tmpdir } = require('os');
3030
const { join, resolve, relative, matchesGlob } = require('path');
3131
const { fileURLToPath } = require('internal/url');
3232
const { kMappings, SourceMap } = require('internal/source_map/source_map');
33+
const {
34+
codes: {
35+
ERR_SOURCE_MAP_MISSING_SOURCE,
36+
},
37+
} = require('internal/errors');
3338
const kCoverageFileRegex = /^coverage-(\d+)-(\d{13})-(\d+)\.json$/;
3439
const kIgnoreRegex = /\/\* node:coverage ignore next (?<count>\d+ )?\*\//;
3540
const kLineEndingRegex = /\r?\n$/u;
@@ -390,6 +395,9 @@ class TestCoverage {
390395

391396
newUrl ??= startEntry?.originalSource;
392397
const mappedLines = this.getLines(newUrl);
398+
if (!mappedLines) {
399+
throw new ERR_SOURCE_MAP_MISSING_SOURCE(newUrl, url);
400+
}
393401
const mappedStartOffset = this.entryToOffset(startEntry, mappedLines);
394402
const mappedEndOffset = this.entryToOffset(endEntry, mappedLines) + 1;
395403
for (let l = startEntry.originalLine; l <= endEntry.originalLine; l++) {
File renamed without changes.
File renamed without changes.
File renamed without changes.

test/fixtures/test-runner/source-map/missing-sources/index.js

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

test/fixtures/test-runner/source-map/missing-sources/index.js.map

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

test/parallel/test-runner-coverage.js

Lines changed: 28 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ const { readdirSync } = require('node:fs');
66
const { test } = require('node:test');
77
const fixtures = require('../common/fixtures');
88
const tmpdir = require('../common/tmpdir');
9+
const { pathToFileURL } = require('node:url');
910
const skipIfNoInspector = {
1011
skip: !process.features.inspector ? 'inspector disabled' : false
1112
};
@@ -320,6 +321,20 @@ test('coverage with source maps', skipIfNoInspector, () => {
320321
assert.strictEqual(result.status, 1);
321322
});
322323

324+
test('coverage with source maps missing sources', skipIfNoInspector, () => {
325+
const file = fixtures.path('test-runner', 'source-map', 'missing-sources', 'index.js');
326+
const missing = fixtures.path('test-runner', 'source-map', 'missing-sources', 'nonexistent.js');
327+
const result = spawnSync(process.execPath, [
328+
'--test',
329+
'--experimental-test-coverage',
330+
file,
331+
]);
332+
333+
const error = `Cannot find '${pathToFileURL(missing)}' imported from the source map for '${pathToFileURL(file)}'`;
334+
assert(result.stdout.toString().includes(error));
335+
assert.strictEqual(result.status, 1);
336+
});
337+
323338
test('coverage with ESM hook - source irrelevant', skipIfNoInspector, () => {
324339
let report = [
325340
'# start of coverage report',
@@ -487,24 +502,25 @@ test('coverage with included and excluded files', skipIfNoInspector, () => {
487502
});
488503

489504
test('properly accounts for line endings in source maps', skipIfNoInspector, () => {
490-
const fixture = fixtures.path('test-runner', 'source-map-line-lengths', 'index.js');
505+
const fixture = fixtures.path('test-runner', 'source-map', 'line-lengths', 'index.js');
491506
const args = [
492507
'--test', '--experimental-test-coverage', '--test-reporter', 'tap',
493508
fixture,
494509
];
495510
const report = [
496511
'# start of coverage report',
497-
'# ----------------------------------------------------------------------------',
498-
'# file | line % | branch % | funcs % | uncovered lines',
499-
'# ----------------------------------------------------------------------------',
500-
'# test | | | | ',
501-
'# fixtures | | | | ',
502-
'# test-runner | | | | ',
503-
'# source-map-line-lengths | | | | ',
504-
'# index.ts | 100.00 | 100.00 | 100.00 | ',
505-
'# ----------------------------------------------------------------------------',
506-
'# all files | 100.00 | 100.00 | 100.00 | ',
507-
'# ----------------------------------------------------------------------------',
512+
'# ------------------------------------------------------------------',
513+
'# file | line % | branch % | funcs % | uncovered lines',
514+
'# ------------------------------------------------------------------',
515+
'# test | | | | ',
516+
'# fixtures | | | | ',
517+
'# test-runner | | | | ',
518+
'# source-map | | | | ',
519+
'# line-lengths | | | | ',
520+
'# index.ts | 100.00 | 100.00 | 100.00 | ',
521+
'# ------------------------------------------------------------------',
522+
'# all files | 100.00 | 100.00 | 100.00 | ',
523+
'# ------------------------------------------------------------------',
508524
'# end of coverage report',
509525
].join('\n');
510526

0 commit comments

Comments
 (0)