Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test_runner: report error on missing sourcemap source #55037

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions doc/api/errors.md
Original file line number Diff line number Diff line change
Expand Up @@ -2573,6 +2573,12 @@ disconnected socket.

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

<a id="ERR_SOURCE_MAP_MISSING_SOURCE"></a>

### `ERR_SOURCE_MAP_MISSING_SOURCE`

A file imported from a source map was not found.

<a id="ERR_SQLITE_ERROR"></a>

### `ERR_SQLITE_ERROR`
Expand Down
1 change: 1 addition & 0 deletions lib/internal/errors.js
Original file line number Diff line number Diff line change
Expand Up @@ -1704,6 +1704,7 @@ E('ERR_SOCKET_CONNECTION_TIMEOUT',
E('ERR_SOCKET_DGRAM_IS_CONNECTED', 'Already connected', Error);
E('ERR_SOCKET_DGRAM_NOT_CONNECTED', 'Not connected', Error);
E('ERR_SOCKET_DGRAM_NOT_RUNNING', 'Not running', Error);
E('ERR_SOURCE_MAP_MISSING_SOURCE', `Cannot find '%s' imported from the source map for '%s'`, Error);
E('ERR_SRI_PARSE',
'Subresource Integrity string %j had an unexpected %j at position %d',
SyntaxError);
Expand Down
8 changes: 8 additions & 0 deletions lib/internal/test_runner/coverage.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,11 @@ const { tmpdir } = require('os');
const { join, resolve, relative, matchesGlob } = require('path');
const { fileURLToPath } = require('internal/url');
const { kMappings, SourceMap } = require('internal/source_map/source_map');
const {
codes: {
ERR_SOURCE_MAP_MISSING_SOURCE,
},
} = require('internal/errors');
const kCoverageFileRegex = /^coverage-(\d+)-(\d{13})-(\d+)\.json$/;
const kIgnoreRegex = /\/\* node:coverage ignore next (?<count>\d+ )?\*\//;
const kLineEndingRegex = /\r?\n$/u;
Expand Down Expand Up @@ -390,6 +395,9 @@ class TestCoverage {

newUrl ??= startEntry?.originalSource;
const mappedLines = this.getLines(newUrl);
if (!mappedLines) {
throw new ERR_SOURCE_MAP_MISSING_SOURCE(newUrl, url);
}
const mappedStartOffset = this.entryToOffset(startEntry, mappedLines);
const mappedEndOffset = this.entryToOffset(endEntry, mappedLines) + 1;
for (let l = startEntry.originalLine; l <= endEntry.originalLine; l++) {
Expand Down
2 changes: 2 additions & 0 deletions test/fixtures/test-runner/source-map/missing-sources/index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

40 changes: 28 additions & 12 deletions test/parallel/test-runner-coverage.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ const { readdirSync } = require('node:fs');
const { test } = require('node:test');
const fixtures = require('../common/fixtures');
const tmpdir = require('../common/tmpdir');
const { pathToFileURL } = require('node:url');
const skipIfNoInspector = {
skip: !process.features.inspector ? 'inspector disabled' : false
};
Expand Down Expand Up @@ -320,6 +321,20 @@ test('coverage with source maps', skipIfNoInspector, () => {
assert.strictEqual(result.status, 1);
});

test('coverage with source maps missing sources', skipIfNoInspector, () => {
const file = fixtures.path('test-runner', 'source-map', 'missing-sources', 'index.js');
const missing = fixtures.path('test-runner', 'source-map', 'missing-sources', 'nonexistent.js');
const result = spawnSync(process.execPath, [
'--test',
'--experimental-test-coverage',
file,
]);

const error = `Cannot find '${pathToFileURL(missing)}' imported from the source map for '${pathToFileURL(file)}'`;
assert(result.stdout.toString().includes(error));
assert.strictEqual(result.status, 1);
});

test('coverage with ESM hook - source irrelevant', skipIfNoInspector, () => {
let report = [
'# start of coverage report',
Expand Down Expand Up @@ -487,24 +502,25 @@ test('coverage with included and excluded files', skipIfNoInspector, () => {
});

test('properly accounts for line endings in source maps', skipIfNoInspector, () => {
const fixture = fixtures.path('test-runner', 'source-map-line-lengths', 'index.js');
const fixture = fixtures.path('test-runner', 'source-map', 'line-lengths', 'index.js');
const args = [
'--test', '--experimental-test-coverage', '--test-reporter', 'tap',
fixture,
];
const report = [
'# start of coverage report',
'# ----------------------------------------------------------------------------',
'# file | line % | branch % | funcs % | uncovered lines',
'# ----------------------------------------------------------------------------',
'# test | | | | ',
'# fixtures | | | | ',
'# test-runner | | | | ',
'# source-map-line-lengths | | | | ',
'# index.ts | 100.00 | 100.00 | 100.00 | ',
'# ----------------------------------------------------------------------------',
'# all files | 100.00 | 100.00 | 100.00 | ',
'# ----------------------------------------------------------------------------',
'# ------------------------------------------------------------------',
'# file | line % | branch % | funcs % | uncovered lines',
'# ------------------------------------------------------------------',
'# test | | | | ',
'# fixtures | | | | ',
'# test-runner | | | | ',
'# source-map | | | | ',
'# line-lengths | | | | ',
'# index.ts | 100.00 | 100.00 | 100.00 | ',
'# ------------------------------------------------------------------',
'# all files | 100.00 | 100.00 | 100.00 | ',
'# ------------------------------------------------------------------',
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Review note

I moved source-map-line-lengths into source-map/line-lengths, hence this change.

'# end of coverage report',
].join('\n');

Expand Down
Loading