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
65 changes: 53 additions & 12 deletions test/common/assertSnapshot.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,17 @@ const path = require('node:path');
const test = require('node:test');
const fs = require('node:fs/promises');
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;
const windowNewlineRegexp = /\r/g;

// Replaces the current Node.js executable version strings with a
// placeholder. This could commonly present in an unhandled exception
// output.
function replaceNodeVersion(str) {
return str.replaceAll(process.version, '*');
return str.replaceAll(process.version, '<node-version>');
}

function replaceStackTrace(str, replacement = '$1*$7$8\n') {
Expand All @@ -23,18 +27,45 @@ function replaceInternalStackTrace(str) {
return str.replaceAll(/(\W+).*[(\s]node:.*/g, '$1*');
}

// Replaces Windows line endings with posix line endings for unified snapshots
// across platforms.
function replaceWindowsLineEndings(str) {
return str.replace(windowNewlineRegexp, '');
}

// Replaces all Windows path separators with posix separators for unified snapshots
// across platforms.
function replaceWindowsPaths(str) {
return common.isWindows ? str.replaceAll(path.win32.sep, path.posix.sep) : str;
}

function transformProjectRoot(replacement = '') {
// Removes line trailing white spaces.
function replaceTrailingSpaces(str) {
return str.replaceAll(/[\t ]+\n/g, '\n');
}

// Replaces customized or platform specific executable names to be `<node-exe>`.
function generalizeExeName(str) {
const baseName = path.basename(process.argv0 || 'node', '.exe');
return str.replaceAll(`${baseName} --`, '<node-exe> --');
}

// Replaces the pids in warning messages with a placeholder.
function replaceWarningPid(str) {
return str.replaceAll(/\(node:\d+\)/g, '(node:<pid>)');
}

// Replaces path strings representing the nodejs/node repo full project root with
// `<project-root>`. Also replaces file URLs containing the full project root path.
// The project root path may contain unicode characters.
function transformProjectRoot(replacement = '<project-root>') {
const projectRoot = path.resolve(__dirname, '../..');
// Handles URL encoded project root in file URL strings as well.
const urlEncoded = pathToFileURL(projectRoot).pathname;
return (str) => {
return str.replaceAll('\\\'', "'").replaceAll(projectRoot, replacement);
return str.replaceAll('\\\'', "'")
.replaceAll(projectRoot, replacement)
.replaceAll(urlEncoded, replacement);
};
}

Expand Down Expand Up @@ -152,32 +183,41 @@ function pickTestFileFromLcov(str) {
);
}

const defaultTransform = transform(
// Transforms basic patterns like:
// - platform specific path and line endings,
// - line trailing spaces,
// - executable specific path and versions.
const basicTransform = transform(
replaceWindowsLineEndings,
replaceStackTrace,
replaceTrailingSpaces,
removeWindowsPathEscaping,
transformProjectRoot(),
replaceWindowsPaths,
replaceNodeVersion,
generalizeExeName,
replaceWarningPid,
);

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

Expand All @@ -204,6 +244,7 @@ module.exports = {
transform,
transformProjectRoot,
replaceTestDuration,
basicTransform,
defaultTransform,
specTransform,
junitTransform,
Expand Down
4 changes: 2 additions & 2 deletions test/fixtures/console/stack_overflow.snapshot
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
before
*test*fixtures*console*stack_overflow.js:*
<project-root>/test/fixtures/console/stack_overflow.js:*
JSON.stringify(array);
^

[RangeError: Maximum call stack size exceeded]

Node.js *
Node.js <node-version>
10 changes: 5 additions & 5 deletions test/fixtures/errors/async_error_eval_esm.snapshot
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Error: test
at one (file:*/[eval1]:2:9)
at two (file:*/[eval1]:15:9)
at async three (file:*/[eval1]:18:3)
at async four (file:*/[eval1]:22:3)
at async main (file:*/[eval1]:28:5)
at one (file://<project-root>/[eval1]:2:9)
at two (file://<project-root>/[eval1]:15:9)
at async three (file://<project-root>/[eval1]:18:3)
at async four (file://<project-root>/[eval1]:22:3)
at async main (file://<project-root>/[eval1]:28:5)

10 changes: 5 additions & 5 deletions test/fixtures/errors/async_error_microtask_main.snapshot
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Error: test
at one (*fixtures*async-error.js:4:9)
at two (*fixtures*async-error.js:17:9)
at async three (*fixtures*async-error.js:20:3)
at async four (*fixtures*async-error.js:24:3)
at async main (*async_error_microtask_main.js:7:5)
at one (<project-root>/test/fixtures/async-error.js:4:9)
at two (<project-root>/test/fixtures/async-error.js:17:9)
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_microtask_main.js:7:5)
12 changes: 6 additions & 6 deletions 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 (*fixtures*async-error.js:4:9)
at two (*fixtures*async-error.js:17:9)
at process.processTicksAndRejections (node:internal*process*task_queues:104:5)
at async three (*fixtures*async-error.js:20:3)
at async four (*fixtures*async-error.js:24:3)
at async main (*async_error_nexttick_main.js:7:5)
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 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)
10 changes: 5 additions & 5 deletions test/fixtures/errors/async_error_sync_esm.snapshot
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Error: test
at one (*fixtures*async-error.js:4:9)
at two (*fixtures*async-error.js:17:9)
at async three (*fixtures*async-error.js:20:3)
at async four (*fixtures*async-error.js:24:3)
at async main (file:*/async_error_sync_esm.mjs:6:5)
at one (<project-root>/test/fixtures/async-error.js:4:9)
at two (<project-root>/test/fixtures/async-error.js:17:9)
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 (file://<project-root>/test/fixtures/errors/async_error_sync_esm.mjs:6:5)
10 changes: 5 additions & 5 deletions test/fixtures/errors/async_error_sync_main.snapshot
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Error: test
at one (*fixtures*async-error.js:4:9)
at two (*fixtures*async-error.js:17:9)
at async three (*fixtures*async-error.js:20:3)
at async four (*fixtures*async-error.js:24:3)
at async main (*async_error_sync_main.js:7:5)
at one (<project-root>/test/fixtures/async-error.js:4:9)
at two (<project-root>/test/fixtures/async-error.js:17:9)
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_sync_main.js:7:5)
4 changes: 2 additions & 2 deletions test/fixtures/errors/core_line_numbers.snapshot
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@ node:punycode:54
RangeError: Invalid input
at error (node:punycode:54:8)
at Object.decode (node:punycode:247:5)
at Object.<anonymous> (*core_line_numbers.js:13:10)
at Object.<anonymous> (<project-root>/test/fixtures/errors/core_line_numbers.js:13:10)

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

AggregateError: original
at Object.<anonymous> (*error_aggregateTwoErrors.js:*:*) {
at Object.<anonymous> (<project-root>/test/fixtures/errors/error_aggregateTwoErrors.js:*:*) {
code: 'ERR0',
[errors]: [
Error: original
at Object.<anonymous> (*error_aggregateTwoErrors.js:*:*) {
at Object.<anonymous> (<project-root>/test/fixtures/errors/error_aggregateTwoErrors.js:*:*) {
code: 'ERR0'
},
Error: second error
at Object.<anonymous> (*error_aggregateTwoErrors.js:*:*) {
at Object.<anonymous> (<project-root>/test/fixtures/errors/error_aggregateTwoErrors.js:*:*) {
code: 'ERR1'
}
]
}

Node.js *
Node.js <node-version>
6 changes: 3 additions & 3 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:*
throw error;
^

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

1 !== 2

at Object.<anonymous> (*error_exit.js:*:*) {
at Object.<anonymous> (<project-root>/test/fixtures/errors/error_exit.js:*:*) {
generatedMessage: true,
code: 'ERR_ASSERTION',
actual: 1,
Expand All @@ -16,4 +16,4 @@ AssertionError [ERR_ASSERTION]: Expected values to be strictly equal:
diff: 'simple'
}

Node.js *
Node.js <node-version>
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:*
throw er; * Unhandled 'error' event
throw er; // Unhandled 'error' event
^

Error: foo:bar
at bar (*events_unhandled_error_common_trace.js:*:*)
at foo (*events_unhandled_error_common_trace.js:*:*)
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:*:*)
Emitted 'error' event at:
at quux (*events_unhandled_error_common_trace.js:*:*)
at Object.<anonymous> (*events_unhandled_error_common_trace.js:*:*)
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:*:*)

Node.js *
Node.js <node-version>
8 changes: 4 additions & 4 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:*
throw er; * Unhandled 'error' event
throw er; // Unhandled 'error' event
^

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

Node.js *
Node.js <node-version>
8 changes: 4 additions & 4 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:*
throw er; * Unhandled 'error' event
throw er; // Unhandled 'error' event
^

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

Node.js *
Node.js <node-version>
8 changes: 4 additions & 4 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:*
throw er; * Unhandled 'error' event
throw er; // Unhandled 'error' event
^

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

Node.js *
Node.js <node-version>
6 changes: 3 additions & 3 deletions test/fixtures/errors/force_colors.snapshot
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
*force_colors.js:2
<project-root>/test/fixtures/errors/force_colors.js:2
throw new Error('Should include grayed stack trace');
^

Error: Should include grayed stack trace
at Object.<anonymous> (/test*force_colors.js:2:7)
at Object.<anonymous> (<project-root>/test/fixtures/errors/force_colors.js:2:7)
 at *
 at *
 at *
Expand All @@ -12,4 +12,4 @@ Error: Should include grayed stack trace
 at *
 at *

Node.js *
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
Expand Up @@ -3,24 +3,24 @@ node:assert:*
^

AssertionError [ERR_ASSERTION]: ifError got unwanted exception: test error
at z (*if-error-has-good-stack.js:*:*)
at y (*if-error-has-good-stack.js:*:*)
at x (*if-error-has-good-stack.js:*:*)
at Object.<anonymous> (*if-error-has-good-stack.js:*:*)
at c (*if-error-has-good-stack.js:*:*)
at b (*if-error-has-good-stack.js:*:*)
at a (*if-error-has-good-stack.js:*:*)
at Object.<anonymous> (*if-error-has-good-stack.js:*:*) {
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:*:*) {
generatedMessage: false,
code: 'ERR_ASSERTION',
actual: Error: test error
at c (*if-error-has-good-stack.js:*:*)
at b (*if-error-has-good-stack.js:*:*)
at a (*if-error-has-good-stack.js:*:*)
at Object.<anonymous> (*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:*:*),
expected: null,
operator: 'ifError',
diff: 'simple'
}

Node.js *
Node.js <node-version>
4 changes: 2 additions & 2 deletions test/fixtures/errors/promise_always_throw_unhandled.snapshot
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
*promise_always_throw_unhandled.js:*
<project-root>/test/fixtures/errors/promise_always_throw_unhandled.js:*
throw new Error('One');
^

Expand All @@ -14,4 +14,4 @@ Error: One
at *
at *

Node.js *
Node.js <node-version>
Loading
Loading