Skip to content

Commit 8b5916d

Browse files
cjihrigjcbhmr
authored andcommitted
test_runner: support source mapped test locations
This commit adds support for source mapping test locations when the --enable-source-maps flag is present. Fixes: nodejs#51392 PR-URL: nodejs#52010 Fixes: nodejs#51610 Reviewed-By: Benjamin Gruenbaum <[email protected]> Reviewed-By: Marco Ippolito <[email protected]> Reviewed-By: Chengzhong Wu <[email protected]> Reviewed-By: Chemi Atlow <[email protected]> Reviewed-By: Moshe Atlow <[email protected]>
1 parent c402e4f commit 8b5916d

File tree

7 files changed

+72
-1
lines changed

7 files changed

+72
-1
lines changed

lib/internal/test_runner/test.js

+21-1
Original file line numberDiff line numberDiff line change
@@ -76,8 +76,17 @@ const kHookNames = ObjectSeal(['before', 'after', 'beforeEach', 'afterEach']);
7676
const kUnwrapErrors = new SafeSet()
7777
.add(kTestCodeFailure).add(kHookFailure)
7878
.add('uncaughtException').add('unhandledRejection');
79-
const { testNamePatterns, testOnlyFlag } = parseCommandLine();
79+
const { sourceMaps, testNamePatterns, testOnlyFlag } = parseCommandLine();
8080
let kResistStopPropagation;
81+
let findSourceMap;
82+
83+
function lazyFindSourceMap(file) {
84+
if (findSourceMap === undefined) {
85+
({ findSourceMap } = require('internal/source_map/source_map_cache'));
86+
}
87+
88+
return findSourceMap(file);
89+
}
8190

8291
function stopTest(timeout, signal) {
8392
const deferred = createDeferredPromise();
@@ -363,6 +372,17 @@ class Test extends AsyncResource {
363372
column: loc[1],
364373
file: loc[2],
365374
};
375+
376+
if (sourceMaps === true) {
377+
const map = lazyFindSourceMap(this.loc.file);
378+
const entry = map?.findEntry(this.loc.line - 1, this.loc.column - 1);
379+
380+
if (entry !== undefined) {
381+
this.loc.line = entry.originalLine + 1;
382+
this.loc.column = entry.originalColumn + 1;
383+
this.loc.file = entry.originalSource;
384+
}
385+
}
366386
}
367387
}
368388

lib/internal/test_runner/utils.js

+2
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,7 @@ function parseCommandLine() {
193193

194194
const isTestRunner = getOptionValue('--test');
195195
const coverage = getOptionValue('--experimental-test-coverage');
196+
const sourceMaps = getOptionValue('--enable-source-maps');
196197
const isChildProcess = process.env.NODE_TEST_CONTEXT === 'child';
197198
const isChildProcessV8 = process.env.NODE_TEST_CONTEXT === 'child-v8';
198199
let destinations;
@@ -244,6 +245,7 @@ function parseCommandLine() {
244245
__proto__: null,
245246
isTestRunner,
246247
coverage,
248+
sourceMaps,
247249
testOnlyFlag,
248250
testNamePatterns,
249251
reporters,
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
// Flags: --enable-source-maps
2+
import { test } from 'node:test';
3+
import { strictEqual } from 'node:assert';
4+
test('fails', () => {
5+
strictEqual(1, 2);
6+
});
7+
//# sourceMappingURL=source_mapped_locations.mjs.map

test/fixtures/test-runner/output/source_mapped_locations.mjs.map

+1
Original file line numberDiff line numberDiff line change
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
TAP version 13
2+
# Subtest: fails
3+
not ok 1 - fails
4+
---
5+
duration_ms: *
6+
location: 'file:///test/fixtures/test-runner/output/source_mapped_locations.ts:5:1'
7+
failureType: 'testCodeFailure'
8+
error: |-
9+
Expected values to be strictly equal:
10+
11+
1 !== 2
12+
13+
code: 'ERR_ASSERTION'
14+
name: 'AssertionError'
15+
expected: 2
16+
actual: 1
17+
operator: 'strictEqual'
18+
stack: |-
19+
*
20+
*
21+
*
22+
*
23+
*
24+
...
25+
1..1
26+
# tests 1
27+
# suites 0
28+
# pass 0
29+
# fail 1
30+
# cancelled 0
31+
# skipped 0
32+
# todo 0
33+
# duration_ms *
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
// Flags: --enable-source-maps
2+
import { test } from 'node:test';
3+
import { strictEqual } from 'node:assert';
4+
5+
test('fails', () => {
6+
strictEqual(1, 2);
7+
});

test/parallel/test-runner-output.mjs

+1
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,7 @@ const tests = [
107107
{ name: 'test-runner/output/spec_reporter_successful.js', transform: specTransform },
108108
{ name: 'test-runner/output/spec_reporter.js', transform: specTransform },
109109
{ name: 'test-runner/output/spec_reporter_cli.js', transform: specTransform },
110+
{ name: 'test-runner/output/source_mapped_locations.mjs' },
110111
process.features.inspector ? { name: 'test-runner/output/lcov_reporter.js', transform: lcovTransform } : false,
111112
{ name: 'test-runner/output/output.js' },
112113
{ name: 'test-runner/output/output_cli.js' },

0 commit comments

Comments
 (0)