Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
86 changes: 58 additions & 28 deletions test/common/assertSnapshot.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,25 @@ const assert = require('node:assert/strict');
const { pathToFileURL } = require('node:url');
const { hostname } = require('node:os');

const stackFramesRegexp = /(?<=\n)(\s+)((.+?)\s+\()?(?:\(?(.+?):(\d+)(?::(\d+))?)\)?(\s+\{)?(\[\d+m)?(\n|$)/g;
/* eslint-disable @stylistic/js/max-len,no-control-regex */
/**
* Group 1: Line start (including color codes and escapes)
* Group 2: Function name
* Group 3: Filename
* Group 4: Line number
* Group 5: Column number
* Group 6: Line end (including color codes and `{` which indicates the start of an error object details)
*/
// Mappings: (g1 ) (g2 ) (g3 ) (g4 ) (g5 ) (g6 )
const internalStackFramesRegexp = /(?<=\n)(\s*(?:\x1b?\[\d+m\s+)?(?:at\s)?)(?:(.+?)\s+\()?(?:(node:.+?):(\d+)(?::(\d+))?)\)?((?:\x1b?\[\d+m)?\s*{?\n|$)/g;
/**
* Group 1: Filename
* Group 2: Line number
* Group 3: Line end and source code line
*/
const internalErrorSourceLines = /(?<=\n|^)(node:.+?):(\d+)(\n.*\n\s*\^(?:\n|$))/g;
/* eslint-enable @stylistic/js/max-len,no-control-regex */

const windowNewlineRegexp = /\r/g;

// Replaces the current Node.js executable version strings with a
Expand All @@ -17,14 +35,33 @@ function replaceNodeVersion(str) {
return str.replaceAll(process.version, '<node-version>');
}

function replaceStackTrace(str, replacement = '$1*$7$8\n') {
return str.replace(stackFramesRegexp, replacement);
// Collapse consecutive identical lines containing the keyword into
// one single line. The `str` should have been processed by `replaceWindowsLineEndings`.
function foldIdenticalLines(str, keyword) {
const lines = str.split('\n');
const folded = lines.filter((line, idx) => {
if (idx === 0) {
return true;
}
if (line.includes(keyword) && line === lines[idx - 1]) {
return false;
}
return true;
});
return folded.join('\n');
}

const kInternalFrame = '<node-internal-frames>';
// Replace non-internal frame `at TracingChannel.traceSync (node:diagnostics_channel:328:14)`
// as well as `at node:internal/main/run_main_module:33:47` with `at <node-internal-frames>`.
// Also replaces error source line like:
// node:internal/mod.js:44
// throw err;
// ^
function replaceInternalStackTrace(str) {
// Replace non-internal frame `at TracingChannel.traceSync (node:diagnostics_channel:328:14)`
// as well as `at node:internal/main/run_main_module:33:47` with `*`.
return str.replaceAll(/(\W+).*[(\s]node:.*/g, '$1*');
const result = str.replaceAll(internalErrorSourceLines, `$1:<line>$3`)
.replaceAll(internalStackFramesRegexp, `$1${kInternalFrame}$6`);
return foldIdenticalLines(result, kInternalFrame);
}

// Replaces Windows line endings with posix line endings for unified snapshots
Expand Down Expand Up @@ -73,6 +110,11 @@ function transformProjectRoot(replacement = '<project-root>') {
};
}

// Replaces tmpdirs created by `test/common/tmpdir.js`.
function transformTmpDir(str) {
return str.replaceAll(/\/\.tmp\.\d+\//g, '/<tmpdir>/');
}

function transform(...args) {
return (str) => args.reduce((acc, fn) => fn(acc), str);
}
Expand Down Expand Up @@ -143,33 +185,24 @@ function replaceTestDuration(str) {
}

const root = path.resolve(__dirname, '..', '..');
const color = '(\\[\\d+m)';
const stackTraceBasePath = new RegExp(`${color}\\(${RegExp.escape(root)}/?${color}(.*)${color}\\)`, 'g');

function replaceSpecDuration(str) {
return str
.replaceAll(/[0-9.]+ms/g, '*ms')
.replaceAll(/duration_ms [0-9.]+/g, 'duration_ms *')
.replace(stackTraceBasePath, '$3');
.replaceAll(/duration_ms [0-9.]+/g, 'duration_ms *');
}

function replaceJunitDuration(str) {
return str
.replaceAll(/time="[0-9.]+"/g, 'time="*"')
.replaceAll(/duration_ms [0-9.]+/g, 'duration_ms *')
.replaceAll(`hostname="${hostname()}"`, 'hostname="HOSTNAME"')
.replaceAll(/file="[^"]*"/g, 'file="*"')
.replace(stackTraceBasePath, '$3');
.replaceAll(/file="[^"]*"/g, 'file="*"');
}

function removeWindowsPathEscaping(str) {
return common.isWindows ? str.replaceAll(/\\\\/g, '\\') : str;
}

function replaceTestLocationLine(str) {
return str.replaceAll(/(js:)(\d+)(:\d+)/g, '$1(LINE)$3');
}

// The Node test coverage returns results for all files called by the test. This
// will make the output file change if files like test/common/index.js change.
// This transform picks only the first line and then the lines from the test
Expand All @@ -188,9 +221,11 @@ function pickTestFileFromLcov(str) {
}

// Transforms basic patterns like:
// - platform specific path and line endings,
// - line trailing spaces,
// - executable specific path and versions.
// - platform specific path and line endings
// - line trailing spaces
// - executable specific path and versions
// - project root path and tmpdir
// - node internal stack frames
const basicTransform = transform(
replaceWindowsLineEndings,
replaceTrailingSpaces,
Expand All @@ -199,29 +234,25 @@ const basicTransform = transform(
replaceNodeVersion,
generalizeExeName,
replaceWarningPid,
transformProjectRoot(),
transformTmpDir,
replaceInternalStackTrace,
);

const defaultTransform = transform(
basicTransform,
replaceStackTrace,
transformProjectRoot(),
replaceTestDuration,
replaceTestLocationLine,
);
const specTransform = transform(
replaceSpecDuration,
basicTransform,
replaceStackTrace,
);
const junitTransform = transform(
replaceJunitDuration,
basicTransform,
replaceStackTrace,
);
const lcovTransform = transform(
basicTransform,
replaceStackTrace,
transformProjectRoot(),
pickTestFileFromLcov,
);

Expand All @@ -240,7 +271,6 @@ module.exports = {
assertSnapshot,
getSnapshotPath,
replaceNodeVersion,
replaceStackTrace,
replaceInternalStackTrace,
replaceWindowsLineEndings,
replaceWindowsPaths,
Expand Down
10 changes: 2 additions & 8 deletions test/fixtures/console/console.snapshot
Original file line number Diff line number Diff line change
@@ -1,9 +1,3 @@
Trace: foo
at *
at *
at *
at *
at *
at *
at *
at *
at Object.<anonymous> (<project-root>/test/fixtures/console/console.js:5:9)
at <node-internal-frames>
2 changes: 1 addition & 1 deletion test/fixtures/console/stack_overflow.snapshot
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
before
<project-root>/test/fixtures/console/stack_overflow.js:*
<project-root>/test/fixtures/console/stack_overflow.js:39
JSON.stringify(array);
^

Expand Down
2 changes: 1 addition & 1 deletion test/fixtures/errors/async_error_nexttick_main.snapshot
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Error: test
at one (<project-root>/test/fixtures/async-error.js:4:9)
at two (<project-root>/test/fixtures/async-error.js:17:9)
at process.processTicksAndRejections (node:internal/process/task_queues:104:5)
at <node-internal-frames>
at async three (<project-root>/test/fixtures/async-error.js:20:3)
at async four (<project-root>/test/fixtures/async-error.js:24:3)
at async main (<project-root>/test/fixtures/errors/async_error_nexttick_main.js:7:5)
5 changes: 2 additions & 3 deletions test/fixtures/errors/core_line_numbers.snapshot
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
node:punycode:54
node:punycode:<line>
throw new RangeError(errors[type]);
^

RangeError: Invalid input
at error (node:punycode:54:8)
at Object.decode (node:punycode:247:5)
at <node-internal-frames>
at Object.<anonymous> (<project-root>/test/fixtures/errors/core_line_numbers.js:13:10)

Node.js <node-version>
8 changes: 4 additions & 4 deletions test/fixtures/errors/error_aggregateTwoErrors.snapshot
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
<project-root>/test/fixtures/errors/error_aggregateTwoErrors.js:*
<project-root>/test/fixtures/errors/error_aggregateTwoErrors.js:15
throw aggregateTwoErrors(err, originalError);
^

AggregateError: original
at Object.<anonymous> (<project-root>/test/fixtures/errors/error_aggregateTwoErrors.js:*:*) {
at Object.<anonymous> (<project-root>/test/fixtures/errors/error_aggregateTwoErrors.js:15:7) {
code: 'ERR0',
[errors]: [
Error: original
at Object.<anonymous> (<project-root>/test/fixtures/errors/error_aggregateTwoErrors.js:*:*) {
at Object.<anonymous> (<project-root>/test/fixtures/errors/error_aggregateTwoErrors.js:9:23) {
code: 'ERR0'
},
Error: second error
at Object.<anonymous> (<project-root>/test/fixtures/errors/error_aggregateTwoErrors.js:*:*) {
at Object.<anonymous> (<project-root>/test/fixtures/errors/error_aggregateTwoErrors.js:10:13) {
code: 'ERR1'
}
]
Expand Down
4 changes: 2 additions & 2 deletions test/fixtures/errors/error_exit.snapshot
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
Exiting with code=1
node:internal/assert/utils:*
node:internal/assert/utils:<line>
throw error;
^

AssertionError [ERR_ASSERTION]: Expected values to be strictly equal:

1 !== 2

at Object.<anonymous> (<project-root>/test/fixtures/errors/error_exit.js:*:*) {
at Object.<anonymous> (<project-root>/test/fixtures/errors/error_exit.js:32:8) {
generatedMessage: true,
code: 'ERR_ASSERTION',
actual: 1,
Expand Down
Binary file modified test/fixtures/errors/error_with_nul.snapshot
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
node:events:*
node:events:<line>
throw er; // Unhandled 'error' event
^

Error: foo:bar
at bar (<project-root>/test/fixtures/errors/events_unhandled_error_common_trace.js:*:*)
at foo (<project-root>/test/fixtures/errors/events_unhandled_error_common_trace.js:*:*)
at bar (<project-root>/test/fixtures/errors/events_unhandled_error_common_trace.js:9:12)
at foo (<project-root>/test/fixtures/errors/events_unhandled_error_common_trace.js:12:10)
Emitted 'error' event at:
at quux (<project-root>/test/fixtures/errors/events_unhandled_error_common_trace.js:*:*)
at Object.<anonymous> (<project-root>/test/fixtures/errors/events_unhandled_error_common_trace.js:*:*)
at quux (<project-root>/test/fixtures/errors/events_unhandled_error_common_trace.js:19:6)
at Object.<anonymous> (<project-root>/test/fixtures/errors/events_unhandled_error_common_trace.js:22:1)

Node.js <node-version>
6 changes: 3 additions & 3 deletions test/fixtures/errors/events_unhandled_error_nexttick.snapshot
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
node:events:*
node:events:<line>
throw er; // Unhandled 'error' event
^

Error
at Object.<anonymous> (<project-root>/test/fixtures/errors/events_unhandled_error_nexttick.js:*:*)
at Object.<anonymous> (<project-root>/test/fixtures/errors/events_unhandled_error_nexttick.js:6:12)
Emitted 'error' event at:
at <project-root>/test/fixtures/errors/events_unhandled_error_nexttick.js:*:*
at <project-root>/test/fixtures/errors/events_unhandled_error_nexttick.js:8:22

Node.js <node-version>
6 changes: 3 additions & 3 deletions test/fixtures/errors/events_unhandled_error_sameline.snapshot
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
node:events:*
node:events:<line>
throw er; // Unhandled 'error' event
^

Error
at Object.<anonymous> (<project-root>/test/fixtures/errors/events_unhandled_error_sameline.js:*:*)
at Object.<anonymous> (<project-root>/test/fixtures/errors/events_unhandled_error_sameline.js:6:34)
Emitted 'error' event at:
at Object.<anonymous> (<project-root>/test/fixtures/errors/events_unhandled_error_sameline.js:*:*)
at Object.<anonymous> (<project-root>/test/fixtures/errors/events_unhandled_error_sameline.js:6:20)

Node.js <node-version>
6 changes: 3 additions & 3 deletions test/fixtures/errors/events_unhandled_error_subclass.snapshot
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
node:events:*
node:events:<line>
throw er; // Unhandled 'error' event
^

Error
at Object.<anonymous> (<project-root>/test/fixtures/errors/events_unhandled_error_subclass.js:*:*)
at Object.<anonymous> (<project-root>/test/fixtures/errors/events_unhandled_error_subclass.js:7:25)
Emitted 'error' event on Foo instance at:
at Object.<anonymous> (<project-root>/test/fixtures/errors/events_unhandled_error_subclass.js:*:*)
at Object.<anonymous> (<project-root>/test/fixtures/errors/events_unhandled_error_subclass.js:7:11)

Node.js <node-version>
8 changes: 1 addition & 7 deletions test/fixtures/errors/force_colors.snapshot
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,6 @@ throw new Error('Should include grayed stack trace');

Error: Should include grayed stack trace
at Object.<anonymous> (<project-root>/test/fixtures/errors/force_colors.js:2:7)
 at *
 at *
 at *
 at *
 at *
 at *
 at *
 at <node-internal-frames>

Node.js <node-version>
26 changes: 13 additions & 13 deletions test/fixtures/errors/if-error-has-good-stack.snapshot
Original file line number Diff line number Diff line change
@@ -1,23 +1,23 @@
node:assert:*
node:assert:<line>
throw newErr;
^

AssertionError [ERR_ASSERTION]: ifError got unwanted exception: test error
at z (<project-root>/test/fixtures/errors/if-error-has-good-stack.js:*:*)
at y (<project-root>/test/fixtures/errors/if-error-has-good-stack.js:*:*)
at x (<project-root>/test/fixtures/errors/if-error-has-good-stack.js:*:*)
at Object.<anonymous> (<project-root>/test/fixtures/errors/if-error-has-good-stack.js:*:*)
at c (<project-root>/test/fixtures/errors/if-error-has-good-stack.js:*:*)
at b (<project-root>/test/fixtures/errors/if-error-has-good-stack.js:*:*)
at a (<project-root>/test/fixtures/errors/if-error-has-good-stack.js:*:*)
at Object.<anonymous> (<project-root>/test/fixtures/errors/if-error-has-good-stack.js:*:*) {
at z (<project-root>/test/fixtures/errors/if-error-has-good-stack.js:21:14)
at y (<project-root>/test/fixtures/errors/if-error-has-good-stack.js:22:7)
at x (<project-root>/test/fixtures/errors/if-error-has-good-stack.js:23:5)
at Object.<anonymous> (<project-root>/test/fixtures/errors/if-error-has-good-stack.js:24:3)
at c (<project-root>/test/fixtures/errors/if-error-has-good-stack.js:13:13)
at b (<project-root>/test/fixtures/errors/if-error-has-good-stack.js:14:7)
at a (<project-root>/test/fixtures/errors/if-error-has-good-stack.js:15:5)
at Object.<anonymous> (<project-root>/test/fixtures/errors/if-error-has-good-stack.js:16:3) {
generatedMessage: false,
code: 'ERR_ASSERTION',
actual: Error: test error
at c (<project-root>/test/fixtures/errors/if-error-has-good-stack.js:*:*)
at b (<project-root>/test/fixtures/errors/if-error-has-good-stack.js:*:*)
at a (<project-root>/test/fixtures/errors/if-error-has-good-stack.js:*:*)
at Object.<anonymous> (<project-root>/test/fixtures/errors/if-error-has-good-stack.js:*:*),
at c (<project-root>/test/fixtures/errors/if-error-has-good-stack.js:13:13)
at b (<project-root>/test/fixtures/errors/if-error-has-good-stack.js:14:7)
at a (<project-root>/test/fixtures/errors/if-error-has-good-stack.js:15:5)
at Object.<anonymous> (<project-root>/test/fixtures/errors/if-error-has-good-stack.js:16:3),
expected: null,
operator: 'ifError',
diff: 'simple'
Expand Down
14 changes: 4 additions & 10 deletions test/fixtures/errors/promise_always_throw_unhandled.snapshot
Original file line number Diff line number Diff line change
@@ -1,17 +1,11 @@
<project-root>/test/fixtures/errors/promise_always_throw_unhandled.js:*
<project-root>/test/fixtures/errors/promise_always_throw_unhandled.js:10
throw new Error('One');
^

Error: One
at *
at <project-root>/test/fixtures/errors/promise_always_throw_unhandled.js:10:9
at new Promise (<anonymous>)
at *
at *
at *
at *
at *
at *
at *
at *
at Object.<anonymous> (<project-root>/test/fixtures/errors/promise_always_throw_unhandled.js:9:14)
at <node-internal-frames>

Node.js <node-version>
10 changes: 2 additions & 8 deletions test/fixtures/errors/promise_unhandled_warn_with_error.snapshot
Original file line number Diff line number Diff line change
@@ -1,11 +1,5 @@
(node:<pid>) UnhandledPromiseRejectionWarning: Error: alas
at *
at *
at *
at *
at *
at *
at *
at *
at Object.<anonymous> (<project-root>/test/fixtures/errors/promise_unhandled_warn_with_error.js:7:16)
at <node-internal-frames>
(Use `<node-exe> --trace-warnings ...` to show where the warning was created)
(node:<pid>) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 1)
Loading
Loading