Skip to content

Commit 800f7c4

Browse files
Aviv Kelleraduh95
Aviv Keller
authored andcommitted
test_runner: throw on invalid source map
PR-URL: #55055 Reviewed-By: Matteo Collina <matteo.collina@gmail.com> Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
1 parent 0f7e3f0 commit 800f7c4

File tree

7 files changed

+27
-0
lines changed

7 files changed

+27
-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_CORRUPT"></a>
2577+
2578+
### `ERR_SOURCE_MAP_CORRUPT`
2579+
2580+
The source map could not be parsed because it does not exist, or is corrupt.
2581+
25762582
<a id="ERR_SOURCE_MAP_MISSING_SOURCE"></a>
25772583

25782584
### `ERR_SOURCE_MAP_MISSING_SOURCE`

lib/internal/errors.js

+1
Original file line numberDiff line numberDiff line change
@@ -1711,6 +1711,7 @@ E('ERR_SOCKET_CONNECTION_TIMEOUT',
17111711
E('ERR_SOCKET_DGRAM_IS_CONNECTED', 'Already connected', Error);
17121712
E('ERR_SOCKET_DGRAM_NOT_CONNECTED', 'Not connected', Error);
17131713
E('ERR_SOCKET_DGRAM_NOT_RUNNING', 'Not running', Error);
1714+
E('ERR_SOURCE_MAP_CORRUPT', `The source map for '%s' does not exist or is corrupt.`, Error);
17141715
E('ERR_SOURCE_MAP_MISSING_SOURCE', `Cannot find '%s' imported from the source map for '%s'`, Error);
17151716
E('ERR_SRI_PARSE',
17161717
'Subresource Integrity string %j had an unexpected %j at position %d',

lib/internal/test_runner/coverage.js

+2
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_CORRUPT,
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_CORRUPT(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

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

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

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

+14
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 corrupt`;
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)