Skip to content

Commit d3e5123

Browse files
committed
test: unify assertSnapshot common patterns
Unifes assertSnapshot common patterns like platform specific path separators, line ending, line trailing spaces, Node.js version strings, and pids in warning messages.
1 parent 938f175 commit d3e5123

File tree

79 files changed

+542
-378
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

79 files changed

+542
-378
lines changed

test/common/assertSnapshot.js

Lines changed: 34 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ const stackFramesRegexp = /(?<=\n)(\s+)((.+?)\s+\()?(?:\(?(.+?):(\d+)(?::(\d+))?
1010
const windowNewlineRegexp = /\r/g;
1111

1212
function replaceNodeVersion(str) {
13-
return str.replaceAll(process.version, '*');
13+
return str.replaceAll(process.version, '<node-version>');
1414
}
1515

1616
function replaceStackTrace(str, replacement = '$1*$7$8\n') {
@@ -31,6 +31,20 @@ function replaceWindowsPaths(str) {
3131
return common.isWindows ? str.replaceAll(path.win32.sep, path.posix.sep) : str;
3232
}
3333

34+
function replaceTrailingSpaces(str) {
35+
return str.replaceAll(/[\t ]+\n/g, '\n');
36+
}
37+
38+
// Replaces customized or platform specific executable names to be `node`.
39+
function generalizeExeName(str) {
40+
const baseName = path.basename(process.argv0 || 'node', '.exe');
41+
return str.replaceAll(`${baseName} --`, 'node --');
42+
}
43+
44+
function replaceWarningPid(str) {
45+
return str.replaceAll(/\(node:\d+\)/g, '(node:<pid>)');
46+
}
47+
3448
function transformProjectRoot(replacement = '') {
3549
const projectRoot = path.resolve(__dirname, '../..');
3650
return (str) => {
@@ -152,32 +166,41 @@ function pickTestFileFromLcov(str) {
152166
);
153167
}
154168

155-
const defaultTransform = transform(
169+
// Transforms basic patterns like:
170+
// - platform specific path and line endings,
171+
// - line trailing spaces,
172+
// - executable specific path and versions.
173+
const basicTransform = transform(
156174
replaceWindowsLineEndings,
157-
replaceStackTrace,
175+
replaceTrailingSpaces,
158176
removeWindowsPathEscaping,
159-
transformProjectRoot(),
160177
replaceWindowsPaths,
178+
replaceNodeVersion,
179+
generalizeExeName,
180+
replaceWarningPid,
181+
);
182+
183+
const defaultTransform = transform(
184+
basicTransform,
185+
replaceStackTrace,
186+
transformProjectRoot(),
161187
replaceTestDuration,
162188
replaceTestLocationLine,
163189
);
164190
const specTransform = transform(
165191
replaceSpecDuration,
166-
replaceWindowsLineEndings,
192+
basicTransform,
167193
replaceStackTrace,
168-
replaceWindowsPaths,
169194
);
170195
const junitTransform = transform(
171196
replaceJunitDuration,
172-
replaceWindowsLineEndings,
197+
basicTransform,
173198
replaceStackTrace,
174-
replaceWindowsPaths,
175199
);
176200
const lcovTransform = transform(
177-
replaceWindowsLineEndings,
201+
basicTransform,
178202
replaceStackTrace,
179203
transformProjectRoot(),
180-
replaceWindowsPaths,
181204
pickTestFileFromLcov,
182205
);
183206

@@ -204,6 +227,7 @@ module.exports = {
204227
transform,
205228
transformProjectRoot,
206229
replaceTestDuration,
230+
basicTransform,
207231
defaultTransform,
208232
specTransform,
209233
junitTransform,
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
before
2-
*test*fixtures*console*stack_overflow.js:*
2+
/test/fixtures/console/stack_overflow.js:*
33
JSON.stringify(array);
44
^
55

66
[RangeError: Maximum call stack size exceeded]
77

8-
Node.js *
8+
Node.js <node-version>
Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
Error: test
2-
at one (file:*/[eval1]:2:9)
3-
at two (file:*/[eval1]:15:9)
4-
at async three (file:*/[eval1]:18:3)
5-
at async four (file:*/[eval1]:22:3)
6-
at async main (file:*/[eval1]:28:5)
2+
at one (file:///[eval1]:2:9)
3+
at two (file:///[eval1]:15:9)
4+
at async three (file:///[eval1]:18:3)
5+
at async four (file:///[eval1]:22:3)
6+
at async main (file:///[eval1]:28:5)
77

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
Error: test
2-
at one (*fixtures*async-error.js:4:9)
3-
at two (*fixtures*async-error.js:17:9)
4-
at async three (*fixtures*async-error.js:20:3)
5-
at async four (*fixtures*async-error.js:24:3)
6-
at async main (*async_error_microtask_main.js:7:5)
2+
at one (/test/fixtures/async-error.js:4:9)
3+
at two (/test/fixtures/async-error.js:17:9)
4+
at async three (/test/fixtures/async-error.js:20:3)
5+
at async four (/test/fixtures/async-error.js:24:3)
6+
at async main (/test/fixtures/errors/async_error_microtask_main.js:7:5)
Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
Error: test
2-
at one (*fixtures*async-error.js:4:9)
3-
at two (*fixtures*async-error.js:17:9)
4-
at process.processTicksAndRejections (node:internal*process*task_queues:104:5)
5-
at async three (*fixtures*async-error.js:20:3)
6-
at async four (*fixtures*async-error.js:24:3)
7-
at async main (*async_error_nexttick_main.js:7:5)
2+
at one (/test/fixtures/async-error.js:4:9)
3+
at two (/test/fixtures/async-error.js:17:9)
4+
at process.processTicksAndRejections (node:internal/process/task_queues:104:5)
5+
at async three (/test/fixtures/async-error.js:20:3)
6+
at async four (/test/fixtures/async-error.js:24:3)
7+
at async main (/test/fixtures/errors/async_error_nexttick_main.js:7:5)
Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
Error: test
2-
at one (*fixtures*async-error.js:4:9)
3-
at two (*fixtures*async-error.js:17:9)
4-
at async three (*fixtures*async-error.js:20:3)
5-
at async four (*fixtures*async-error.js:24:3)
6-
at async main (file:*/async_error_sync_esm.mjs:6:5)
2+
at one (/test/fixtures/async-error.js:4:9)
3+
at two (/test/fixtures/async-error.js:17:9)
4+
at async three (/test/fixtures/async-error.js:20:3)
5+
at async four (/test/fixtures/async-error.js:24:3)
6+
at async main (file:///test/fixtures/errors/async_error_sync_esm.mjs:6:5)
Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
Error: test
2-
at one (*fixtures*async-error.js:4:9)
3-
at two (*fixtures*async-error.js:17:9)
4-
at async three (*fixtures*async-error.js:20:3)
5-
at async four (*fixtures*async-error.js:24:3)
6-
at async main (*async_error_sync_main.js:7:5)
2+
at one (/test/fixtures/async-error.js:4:9)
3+
at two (/test/fixtures/async-error.js:17:9)
4+
at async three (/test/fixtures/async-error.js:20:3)
5+
at async four (/test/fixtures/async-error.js:24:3)
6+
at async main (/test/fixtures/errors/async_error_sync_main.js:7:5)

test/fixtures/errors/core_line_numbers.snapshot

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,6 @@ node:punycode:54
55
RangeError: Invalid input
66
at error (node:punycode:54:8)
77
at Object.decode (node:punycode:247:5)
8-
at Object.<anonymous> (*core_line_numbers.js:13:10)
8+
at Object.<anonymous> (/test/fixtures/errors/core_line_numbers.js:13:10)
99

10-
Node.js *
10+
Node.js <node-version>
Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,20 @@
1-
*error_aggregateTwoErrors.js:*
1+
/test/fixtures/errors/error_aggregateTwoErrors.js:*
22
throw aggregateTwoErrors(err, originalError);
33
^
44

55
AggregateError: original
6-
at Object.<anonymous> (*error_aggregateTwoErrors.js:*:*) {
6+
at Object.<anonymous> (/test/fixtures/errors/error_aggregateTwoErrors.js:*:*) {
77
code: 'ERR0',
88
[errors]: [
99
Error: original
10-
at Object.<anonymous> (*error_aggregateTwoErrors.js:*:*) {
10+
at Object.<anonymous> (/test/fixtures/errors/error_aggregateTwoErrors.js:*:*) {
1111
code: 'ERR0'
1212
},
1313
Error: second error
14-
at Object.<anonymous> (*error_aggregateTwoErrors.js:*:*) {
14+
at Object.<anonymous> (/test/fixtures/errors/error_aggregateTwoErrors.js:*:*) {
1515
code: 'ERR1'
1616
}
1717
]
1818
}
1919

20-
Node.js *
20+
Node.js <node-version>
Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
Exiting with code=1
2-
node:internal*assert*utils:*
2+
node:internal/assert/utils:*
33
throw error;
44
^
55

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

88
1 !== 2
99

10-
at Object.<anonymous> (*error_exit.js:*:*) {
10+
at Object.<anonymous> (/test/fixtures/errors/error_exit.js:*:*) {
1111
generatedMessage: true,
1212
code: 'ERR_ASSERTION',
1313
actual: 1,
@@ -16,4 +16,4 @@ AssertionError [ERR_ASSERTION]: Expected values to be strictly equal:
1616
diff: 'simple'
1717
}
1818

19-
Node.js *
19+
Node.js <node-version>

0 commit comments

Comments
 (0)