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

[v20.x backport] test_runner: support source mapped test locations #52872

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
28 changes: 27 additions & 1 deletion lib/internal/test_runner/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ const {
PromisePrototypeThen,
PromiseResolve,
SafePromisePrototypeFinally,
StringPrototypeStartsWith,
ReflectApply,
RegExpPrototypeExec,
SafeMap,
Expand Down Expand Up @@ -58,6 +59,7 @@ const {
} = require('internal/validators');
const { setTimeout } = require('timers');
const { TIMEOUT_MAX } = require('internal/timers');
const { fileURLToPath } = require('internal/url');
const { availableParallelism } = require('os');
const { bigint: hrtime } = process.hrtime;
const kCallbackAndPromisePresent = 'callbackAndPromisePresent';
Expand All @@ -76,8 +78,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 @@ -365,6 +376,21 @@ 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;
}
}

if (StringPrototypeStartsWith(this.loc.file, 'file://')) {
this.loc.file = fileURLToPath(this.loc.file);
}
}
}

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: '/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 @@ -114,6 +114,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