Skip to content

Commit

Permalink
Rely on Node.js to apply source maps to stack traces
Browse files Browse the repository at this point in the history
Use the --enable-source-maps option by default, remove source-map-support.

Fixes #2763.
  • Loading branch information
novemberborn committed Sep 18, 2021
1 parent 716d643 commit 66939e7
Show file tree
Hide file tree
Showing 14 changed files with 15 additions and 286 deletions.
6 changes: 4 additions & 2 deletions lib/fork.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ export function _testOnlyReplaceWorkerPath(replacement) {

let forkCounter = 0;

const additionalExecArgv = ['--enable-source-maps'];

const createWorker = (options, execArgv) => {
let worker;
let postMessage;
Expand All @@ -23,7 +25,7 @@ const createWorker = (options, execArgv) => {
worker = new Worker(workerPath, {
argv: options.workerArgv,
env: {NODE_ENV: 'test', ...process.env, ...options.environmentVariables},
execArgv,
execArgv: [...execArgv, ...additionalExecArgv],
workerData: {
options,
},
Expand Down Expand Up @@ -51,7 +53,7 @@ const createWorker = (options, execArgv) => {
cwd: options.projectDir,
silent: true,
env: {NODE_ENV: 'test', ...process.env, ...options.environmentVariables},
execArgv,
execArgv: [...execArgv, ...additionalExecArgv],
});
postMessage = controlFlow(worker);
close = async () => worker.kill();
Expand Down
7 changes: 0 additions & 7 deletions lib/worker/base.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import {pathToFileURL} from 'node:url';
import {workerData} from 'node:worker_threads';

import setUpCurrentlyUnhandled from 'currently-unhandled';
import sourceMapSupport from 'source-map-support';

import {set as setChalk} from '../chalk.js';
import nowAndTimers from '../now-and-timers.cjs';
Expand Down Expand Up @@ -130,12 +129,6 @@ const run = async options => {
// Store value to prevent required modules from modifying it.
const testPath = options.file;

// Install basic source map support.
sourceMapSupport.install({
environment: 'node',
handleUncaughtExceptions: false,
});

const extensionsToLoadAsModules = Object.entries(options.moduleTypes)
.filter(([, type]) => type === 'module')
.map(([extension]) => extension);
Expand Down
9 changes: 2 additions & 7 deletions lib/worker/line-numbers.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,6 @@ module.exports = ({file, lineNumbers = []}) => {
// Avoid loading these until we actually need to select tests by line number.
const {pathToFileURL} = require('url');
const callsites = require('callsites');
const sourceMapSupport = require('source-map-support');

const locations = parse(file);
const selected = new Set(lineNumbers);
Expand All @@ -83,13 +82,9 @@ module.exports = ({file, lineNumbers = []}) => {
return false;
}

// FIXME: This assumes the callSite hasn't already been adjusted. It's likely
// that if `source-map-support/register` has been loaded, this would result
// in the wrong location.
const sourceCallSite = sourceMapSupport.wrapCallSite(callSite);
const start = {
line: sourceCallSite.getLineNumber(),
column: sourceCallSite.getColumnNumber() - 1, // Use 0-indexed columns.
line: callSite.getLineNumber(),
column: callSite.getColumnNumber() - 1, // Use 0-indexed columns.
};

const test = findTest(locations, start);
Expand Down
26 changes: 9 additions & 17 deletions package-lock.json

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

2 changes: 0 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,6 @@
"read-pkg": "^7.0.0",
"resolve-cwd": "^3.0.0",
"slash": "^3.0.0",
"source-map-support": "^0.5.20",
"stack-utils": "^2.0.3",
"strip-ansi": "^7.0.1",
"supertap": "^2.0.0",
Expand All @@ -137,7 +136,6 @@
"it-first": "^1.0.6",
"replace-string": "^4.0.0",
"sinon": "^11.1.2",
"source-map-fixtures": "^2.1.0",
"tap": "^15.0.9",
"temp-write": "^5.0.0",
"tempy": "^2.0.0",
Expand Down
120 changes: 0 additions & 120 deletions test-tap/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -330,54 +330,6 @@ for (const opt of opts) {
});
});

test(`stack traces for exceptions are corrected using a source map file - workerThreads: ${opt.workerThreads}`, async t => {
t.plan(4);

const api = await apiCreator({
...opt,
cacheEnabled: true,
});

api.on('run', plan => {
plan.status.on('stateChange', evt => {
if (evt.type === 'uncaught-exception') {
t.match(evt.err.message, /Thrown by source-map-fixtures/);
t.match(evt.err.stack, /^.*?\brun\b.*source-map-fixtures.src.throws.js:1.*$/m);
t.match(evt.err.stack, /^.*?Immediate\b.*source-map-file.cjs:5.*$/m);
}
});
});

return api.run({files: [path.join(__dirname, 'fixture/source-map-file.cjs')]})
.then(runStatus => {
t.equal(runStatus.stats.passedTests, 1);
});
});

test(`stack traces for exceptions are corrected using a source map file in what looks like a browser env - workerThreads: ${opt.workerThreads}`, async t => {
t.plan(4);

const api = await apiCreator({
...opt,
cacheEnabled: true,
});

api.on('run', plan => {
plan.status.on('stateChange', evt => {
if (evt.type === 'uncaught-exception') {
t.match(evt.err.message, /Thrown by source-map-fixtures/);
t.match(evt.err.stack, /^.*?\brun\b.*source-map-fixtures.src.throws.js:1.*$/m);
t.match(evt.err.stack, /^.*?Immediate\b.*source-map-file-browser-env.cjs:8.*$/m);
}
});
});

return api.run({files: [path.join(__dirname, 'fixture/source-map-file-browser-env.cjs')]})
.then(runStatus => {
t.equal(runStatus.stats.passedTests, 1);
});
});

test(`enhanced assertion formatting necessary whitespace and empty strings - workerThreads: ${opt.workerThreads}`, async t => {
const expected = [
[
Expand Down Expand Up @@ -428,78 +380,6 @@ for (const opt of opts) {
});
});

test(`stack traces for exceptions are corrected using a source map file (cache off) - workerThreads: ${opt.workerThreads}`, async t => {
t.plan(4);

const api = await apiCreator({
...opt,
cacheEnabled: false,
});

api.on('run', plan => {
plan.status.on('stateChange', evt => {
if (evt.type === 'uncaught-exception') {
t.match(evt.err.message, /Thrown by source-map-fixtures/);
t.match(evt.err.stack, /^.*?\brun\b.*source-map-fixtures.src.throws.js:1.*$/m);
t.match(evt.err.stack, /^.*?Immediate\b.*source-map-file.cjs:5.*$/m);
}
});
});

return api.run({files: [path.join(__dirname, 'fixture/source-map-file.cjs')]})
.then(runStatus => {
t.equal(runStatus.stats.passedTests, 1);
});
});

test(`stack traces for exceptions are corrected using a source map, taking an initial source map for the test file into account (cache on) - workerThreads: ${opt.workerThreads}`, async t => {
t.plan(4);

const api = await apiCreator({
...opt,
cacheEnabled: true,
});

api.on('run', plan => {
plan.status.on('stateChange', evt => {
if (evt.type === 'uncaught-exception') {
t.match(evt.err.message, /Thrown by source-map-fixtures/);
t.match(evt.err.stack, /^.*?\brun\b.*source-map-fixtures.src.throws.js:1.*$/m);
t.match(evt.err.stack, /^.*?Immediate\b.*source-map-initial.cjs:23.*$/m);
}
});
});

return api.run({files: [path.join(__dirname, 'fixture/source-map-initial.cjs')]})
.then(runStatus => {
t.equal(runStatus.stats.passedTests, 1);
});
});

test(`stack traces for exceptions are corrected using a source map, taking an initial source map for the test file into account (cache off) - workerThreads: ${opt.workerThreads}`, async t => {
t.plan(4);

const api = await apiCreator({
...opt,
cacheEnabled: false,
});

api.on('run', plan => {
plan.status.on('stateChange', evt => {
if (evt.type === 'uncaught-exception') {
t.match(evt.err.message, /Thrown by source-map-fixtures/);
t.match(evt.err.stack, /^.*?\brun\b.*source-map-fixtures.src.throws.js:1.*$/m);
t.match(evt.err.stack, /^.*?Immediate\b.*source-map-initial.cjs:23.*$/m);
}
});
});

return api.run({files: [path.join(__dirname, 'fixture/source-map-initial.cjs')]})
.then(runStatus => {
t.equal(runStatus.stats.passedTests, 1);
});
});

test(`absolute paths - workerThreads: ${opt.workerThreads}`, async t => {
const api = await apiCreator(opt);

Expand Down
37 changes: 0 additions & 37 deletions test-tap/fixture/_generate-source-map-initial.cjs

This file was deleted.

16 changes: 0 additions & 16 deletions test-tap/fixture/source-map-file-browser-env.cjs

This file was deleted.

13 changes: 0 additions & 13 deletions test-tap/fixture/source-map-file.cjs

This file was deleted.

0 comments on commit 66939e7

Please sign in to comment.