Skip to content

Commit

Permalink
test_runner: support source mapped test locations
Browse files Browse the repository at this point in the history
This commit adds support for source mapping test locations
when the --enable-source-maps flag is present.

Fixes: #51392
PR-URL: #52010
Fixes: #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>
  • Loading branch information
cjihrig authored and marco-ippolito committed May 2, 2024
1 parent 590decf commit 089d2ae
Show file tree
Hide file tree
Showing 7 changed files with 72 additions and 1 deletion.
22 changes: 21 additions & 1 deletion lib/internal/test_runner/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,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();
Expand Down Expand Up @@ -362,6 +371,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;
}
}
}
}

Expand Down
2 changes: 2 additions & 0 deletions lib/internal/test_runner/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,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;
Expand Down Expand Up @@ -250,6 +251,7 @@ function parseCommandLine() {
__proto__: null,
isTestRunner,
coverage,
sourceMaps,
testOnlyFlag,
testNamePatterns,
reporters,
Expand Down
7 changes: 7 additions & 0 deletions test/fixtures/test-runner/output/source_mapped_locations.mjs
Original file line number Diff line number Diff line change
@@ -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

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

33 changes: 33 additions & 0 deletions test/fixtures/test-runner/output/source_mapped_locations.snapshot
Original file line number Diff line number Diff line change
@@ -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 *
7 changes: 7 additions & 0 deletions test/fixtures/test-runner/output/source_mapped_locations.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
// Flags: --enable-source-maps
import { test } from 'node:test';
import { strictEqual } from 'node:assert';

test('fails', () => {
strictEqual(1, 2);
});
1 change: 1 addition & 0 deletions test/parallel/test-runner-output.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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' },
Expand Down

0 comments on commit 089d2ae

Please sign in to comment.