Skip to content

Commit bdeaafd

Browse files
cjihrigMoLow
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 <benjamingr@gmail.com> Reviewed-By: Marco Ippolito <marcoippolito54@gmail.com> Reviewed-By: Chengzhong Wu <legendecas@gmail.com> Reviewed-By: Chemi Atlow <chemi@atlow.co.il> Reviewed-By: Moshe Atlow <moshe@atlow.co.il>
1 parent 5abddb4 commit bdeaafd

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();
@@ -365,6 +374,17 @@ class Test extends AsyncResource {
365374
column: loc[1],
366375
file: loc[2],
367376
};
377+
378+
if (sourceMaps === true) {
379+
const map = lazyFindSourceMap(this.loc.file);
380+
const entry = map?.findEntry(this.loc.line - 1, this.loc.column - 1);
381+
382+
if (entry !== undefined) {
383+
this.loc.line = entry.originalLine + 1;
384+
this.loc.column = entry.originalColumn + 1;
385+
this.loc.file = entry.originalSource;
386+
}
387+
}
368388
}
369389
}
370390

lib/internal/test_runner/utils.js

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

200200
const isTestRunner = getOptionValue('--test');
201201
const coverage = getOptionValue('--experimental-test-coverage');
202+
const sourceMaps = getOptionValue('--enable-source-maps');
202203
const isChildProcess = process.env.NODE_TEST_CONTEXT === 'child';
203204
const isChildProcessV8 = process.env.NODE_TEST_CONTEXT === 'child-v8';
204205
let destinations;
@@ -250,6 +251,7 @@ function parseCommandLine() {
250251
__proto__: null,
251252
isTestRunner,
252253
coverage,
254+
sourceMaps,
253255
testOnlyFlag,
254256
testNamePatterns,
255257
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
@@ -114,6 +114,7 @@ const tests = [
114114
{ name: 'test-runner/output/spec_reporter_successful.js', transform: specTransform },
115115
{ name: 'test-runner/output/spec_reporter.js', transform: specTransform },
116116
{ name: 'test-runner/output/spec_reporter_cli.js', transform: specTransform },
117+
{ name: 'test-runner/output/source_mapped_locations.mjs' },
117118
process.features.inspector ? { name: 'test-runner/output/lcov_reporter.js', transform: lcovTransform } : false,
118119
{ name: 'test-runner/output/output.js' },
119120
{ name: 'test-runner/output/output_cli.js' },

0 commit comments

Comments
 (0)