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: https://github.com/nodejs/node/issues/51392
PR-URL: https://github.com/nodejs/node/pull/52010
Fixes: https://github.com/nodejs/node/issues/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>
This commit is contained in:
cjihrig authored and Node.js GitHub Bot committed 2024-03-11 08:56:16 +00:00
1 parent 592c6907bf
commit 6d625fe616
7 files changed
+72 -1

No files matched your search

+21 -1
View File
@@ -76,8 +76,17 @@ const kHookNames = ObjectSeal(['before', 'after', 'beforeEach', 'afterEach']);
const kUnwrapErrors = new SafeSet()
.add(kTestCodeFailure).add(kHookFailure)
.add('uncaughtException').add('unhandledRejection');
const { testNamePatterns, testOnlyFlag } = parseCommandLine();
const { sourceMaps, testNamePatterns, testOnlyFlag } = parseCommandLine();
let kResistStopPropagation;
let findSourceMap;
function lazyFindSourceMap(file) {
if (findSourceMap === undefined) {
({ findSourceMap } = require('internal/source_map/source_map_cache'));
}
return findSourceMap(file);
}
function stopTest(timeout, signal) {
const deferred = createDeferredPromise();
@@ -363,6 +372,17 @@ class Test extends AsyncResource {
column: loc[1],
file: loc[2],
};
if (sourceMaps === true) {
const map = lazyFindSourceMap(this.loc.file);
const entry = map?.findEntry(this.loc.line - 1, this.loc.column - 1);
if (entry !== undefined) {
this.loc.line = entry.originalLine + 1;
this.loc.column = entry.originalColumn + 1;
this.loc.file = entry.originalSource;
}
}
}
}
+2
View File
@@ -193,6 +193,7 @@ function parseCommandLine() {
const isTestRunner = getOptionValue('--test');
const coverage = getOptionValue('--experimental-test-coverage');
const sourceMaps = getOptionValue('--enable-source-maps');
const isChildProcess = process.env.NODE_TEST_CONTEXT === 'child';
const isChildProcessV8 = process.env.NODE_TEST_CONTEXT === 'child-v8';
let destinations;
@@ -244,6 +245,7 @@ function parseCommandLine() {
__proto__: null,
isTestRunner,
coverage,
sourceMaps,
testOnlyFlag,
testNamePatterns,
reporters,
@@ -0,0 +1,7 @@
// Flags: --enable-source-maps
import { test } from 'node:test';
import { strictEqual } from 'node:assert';
test('fails', () => {
strictEqual(1, 2);
});
//# sourceMappingURL=source_mapped_locations.mjs.map
@@ -0,0 +1 @@
{"version":3,"file":"source_mapped_locations.mjs","sourceRoot":"","sources":["source_mapped_locations.ts"],"names":[],"mappings":"AAAA,8BAA8B;AAC9B,OAAO,EAAE,IAAI,EAAE,MAAM,WAAW,CAAC;AACjC,OAAO,EAAE,WAAW,EAAE,MAAM,aAAa,CAAC;AAE1C,IAAI,CAAC,OAAO,EAAE,GAAG,EAAE;IACjB,WAAW,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC;AACpB,CAAC,CAAC,CAAC"}
@@ -0,0 +1,33 @@
TAP version 13
# Subtest: fails
not ok 1 - fails
---
duration_ms: *
location: 'file:///test/fixtures/test-runner/output/source_mapped_locations.ts:5:1'
failureType: 'testCodeFailure'
error: |-
Expected values to be strictly equal:
1 !== 2
code: 'ERR_ASSERTION'
name: 'AssertionError'
expected: 2
actual: 1
operator: 'strictEqual'
stack: |-
*
*
*
*
*
...
1..1
# tests 1
# suites 0
# pass 0
# fail 1
# cancelled 0
# skipped 0
# todo 0
# duration_ms *
@@ -0,0 +1,7 @@
// Flags: --enable-source-maps
import { test } from 'node:test';
import { strictEqual } from 'node:assert';
test('fails', () => {
strictEqual(1, 2);
});
+1
View File
@@ -107,6 +107,7 @@ const tests = [
{ name: 'test-runner/output/spec_reporter_successful.js', transform: specTransform },
{ name: 'test-runner/output/spec_reporter.js', transform: specTransform },
{ name: 'test-runner/output/spec_reporter_cli.js', transform: specTransform },
{ name: 'test-runner/output/source_mapped_locations.mjs' },
process.features.inspector ? { name: 'test-runner/output/lcov_reporter.js', transform: lcovTransform } : false,
{ name: 'test-runner/output/output.js' },
{ name: 'test-runner/output/output_cli.js' },