Skip to content

Commit a916ce4

Browse files
committed
test_runner: throw on invalid source maps
1 parent 2305fb1 commit a916ce4

File tree

7 files changed

+27
-0
lines changed

7 files changed

+27
-0
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_CANNOT_PARSE"></a>
2577+
2578+
### `ERR_SOURCE_MAP_CANNOT_PARSE`
2579+
2580+
The source map could not be parsed because it is not valid JSON.
2581+
25762582
<a id="ERR_SOURCE_MAP_MISSING_SOURCE"></a>
25772583

25782584
### `ERR_SOURCE_MAP_MISSING_SOURCE`

β€Ž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_CANNOT_PARSE', `The source map for '%s' does not exist or is not valid JSON.`, Error);
17071708
E('ERR_SOURCE_MAP_MISSING_SOURCE', `Cannot find '%s' imported from the source map for '%s'`, Error);
17081709
E('ERR_SRI_PARSE',
17091710
'Subresource Integrity string %j had an unexpected %j at position %d',

β€Žlib/internal/test_runner/coverage.jsβ€Ž

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ const { fileURLToPath } = require('internal/url');
3232
const { kMappings, SourceMap } = require('internal/source_map/source_map');
3333
const {
3434
codes: {
35+
ERR_SOURCE_MAP_CANNOT_PARSE,
3536
ERR_SOURCE_MAP_MISSING_SOURCE,
3637
},
3738
} = require('internal/errors');
@@ -353,6 +354,7 @@ class TestCoverage {
353354
continue;
354355
}
355356
const { data, lineLengths } = sourceMapCache[url];
357+
if (!data) throw new ERR_SOURCE_MAP_CANNOT_PARSE(url);
356358
let offset = 0;
357359
const executedLines = ArrayPrototypeMap(lineLengths, (length, i) => {
358360
const coverageLine = new CoverageLine(i + 1, offset, null, length + 1);

β€Žtest/fixtures/test-runner/source-maps/invalid-json/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-maps/invalid-json/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/fixtures/test-runner/source-maps/missing-map.jsβ€Ž

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-source-map.jsβ€Ž

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,4 +80,18 @@ describe('Coverage with source maps', async () => {
8080
t.assert.ok(spawned.stdout.includes(error));
8181
t.assert.strictEqual(spawned.code, 1);
8282
});
83+
84+
for (const [file, message] of [
85+
[fixtures.path('test-runner', 'source-maps', 'invalid-json', 'index.js'), 'is not valid JSON'],
86+
[fixtures.path('test-runner', 'source-maps', 'missing-map.js'), 'does not exist'],
87+
]) {
88+
await it(`should throw when a source map ${message}`, async (t) => {
89+
const spawned = await common.spawnPromisified(process.execPath, [...flags, file]);
90+
91+
const error = `The source map for '${pathToFileURL(file)}' does not exist or is not valid JSON`;
92+
t.assert.strictEqual(spawned.stderr, '');
93+
t.assert.ok(spawned.stdout.includes(error));
94+
t.assert.strictEqual(spawned.code, 1);
95+
});
96+
}
8397
}).then(common.mustCall());

0 commit comments

Comments
Β (0)