Skip to content

Commit 4f88179

Browse files
avivkellerjaydensericcjihrig
authored
test_runner: report error on missing sourcemap source
Co-Authored-By: Jayden Seric <me@jaydenseric.com> Co-Authored-By: Colin Ihrig <cjihrig@gmail.com> PR-URL: #55037 Reviewed-By: Chemi Atlow <chemi@atlow.co.il> Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
1 parent 8b70e6b commit 4f88179

File tree

6 files changed

+33
-0
lines changed

6 files changed

+33
-0
lines changed

doc/api/errors.md

+6
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

+1
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

+8
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++) {

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

+2
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

+1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

test/parallel/test-runner-coverage.js

+15
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',

0 commit comments

Comments
 (0)