Skip to content

Commit f67151d

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 f67151d

File tree

79 files changed

+548
-379
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

+548
-379
lines changed

test/common/assertSnapshot.js

Lines changed: 40 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,14 @@ const path = require('node:path');
44
const test = require('node:test');
55
const fs = require('node:fs/promises');
66
const assert = require('node:assert/strict');
7+
const { pathToFileURL } = require('node:url');
78
const { hostname } = require('node:os');
89

910
const stackFramesRegexp = /(?<=\n)(\s+)((.+?)\s+\()?(?:\(?(.+?):(\d+)(?::(\d+))?)\)?(\s+\{)?(\[\d+m)?(\n|$)/g;
1011
const windowNewlineRegexp = /\r/g;
1112

1213
function replaceNodeVersion(str) {
13-
return str.replaceAll(process.version, '*');
14+
return str.replaceAll(process.version, '<node-version>');
1415
}
1516

1617
function replaceStackTrace(str, replacement = '$1*$7$8\n') {
@@ -31,10 +32,28 @@ function replaceWindowsPaths(str) {
3132
return common.isWindows ? str.replaceAll(path.win32.sep, path.posix.sep) : str;
3233
}
3334

35+
function replaceTrailingSpaces(str) {
36+
return str.replaceAll(/[\t ]+\n/g, '\n');
37+
}
38+
39+
// Replaces customized or platform specific executable names to be `node`.
40+
function generalizeExeName(str) {
41+
const baseName = path.basename(process.argv0 || 'node', '.exe');
42+
return str.replaceAll(`${baseName} --`, 'node --');
43+
}
44+
45+
function replaceWarningPid(str) {
46+
return str.replaceAll(/\(node:\d+\)/g, '(node:<pid>)');
47+
}
48+
3449
function transformProjectRoot(replacement = '') {
3550
const projectRoot = path.resolve(__dirname, '../..');
51+
// Handles URL encoded project root in file URL strings as well.
52+
const urlEncoded = pathToFileURL(projectRoot).pathname;
3653
return (str) => {
37-
return str.replaceAll('\\\'', "'").replaceAll(projectRoot, replacement);
54+
return str.replaceAll('\\\'', "'")
55+
.replaceAll(projectRoot, replacement)
56+
.replaceAll(urlEncoded, replacement);
3857
};
3958
}
4059

@@ -152,32 +171,41 @@ function pickTestFileFromLcov(str) {
152171
);
153172
}
154173

155-
const defaultTransform = transform(
174+
// Transforms basic patterns like:
175+
// - platform specific path and line endings,
176+
// - line trailing spaces,
177+
// - executable specific path and versions.
178+
const basicTransform = transform(
156179
replaceWindowsLineEndings,
157-
replaceStackTrace,
180+
replaceTrailingSpaces,
158181
removeWindowsPathEscaping,
159-
transformProjectRoot(),
160182
replaceWindowsPaths,
183+
replaceNodeVersion,
184+
generalizeExeName,
185+
replaceWarningPid,
186+
);
187+
188+
const defaultTransform = transform(
189+
basicTransform,
190+
replaceStackTrace,
191+
transformProjectRoot(),
161192
replaceTestDuration,
162193
replaceTestLocationLine,
163194
);
164195
const specTransform = transform(
165196
replaceSpecDuration,
166-
replaceWindowsLineEndings,
197+
basicTransform,
167198
replaceStackTrace,
168-
replaceWindowsPaths,
169199
);
170200
const junitTransform = transform(
171201
replaceJunitDuration,
172-
replaceWindowsLineEndings,
202+
basicTransform,
173203
replaceStackTrace,
174-
replaceWindowsPaths,
175204
);
176205
const lcovTransform = transform(
177-
replaceWindowsLineEndings,
206+
basicTransform,
178207
replaceStackTrace,
179208
transformProjectRoot(),
180-
replaceWindowsPaths,
181209
pickTestFileFromLcov,
182210
);
183211

@@ -204,6 +232,7 @@ module.exports = {
204232
transform,
205233
transformProjectRoot,
206234
replaceTestDuration,
235+
basicTransform,
207236
defaultTransform,
208237
specTransform,
209238
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)