8000 test: unify assertSnapshot common patterns · nodejs/node@b064ddc · GitHub
[go: up one dir, main page]

Skip to content

Commit b064ddc

Browse files
legendecasaduh95
authored andcommitted
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. PR-URL: #61590 Reviewed-By: Moshe Atlow <moshe@atlow.co.il> Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com> Reviewed-By: Gürgün Dayıoğlu <hey@gurgun.day>
1 parent d147c08 commit b064ddc

File tree

100 files changed

+850
-665
lines changed

Some content is hidden

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

100 files changed

+850
-665
lines changed

test/common/assertSnapshot.js

Lines changed: 57 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,17 @@ 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

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

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

30+
// Replaces Windows line endings with posix line endings for unified snapshots
31+
// across platforms.
2632
function replaceWindowsLineEndings(str) {
2733
return str.replace(windowNewlineRegexp, '');
2834
}
2935

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

34-
function transformProjectRoot(replacement = '') {
42+
// Removes line trailing white spaces.
43+
function replaceTrailingSpaces(str) {
44+
return str.replaceAll(/[\t ]+\n/g, '\n');
45+
}
46+
47+
// Replaces customized or platform specific executable names to be `<node-exe>`.
48+
function generalizeExeName(str) {
49+
const baseName = path.basename(process.argv0 || 'node', '.exe');
50+
return str.replaceAll(`${baseName} --`, '<node-exe> --');
51+
}
52+
53+
// Replaces the pids in warning messages with a placeholder.
54+
function replaceWarningPid(str) {
55+
return str.replaceAll(/\(node:\d+\)/g, '(node:<pi B2EA d>)');
56+
}
57+
58+
// Replaces path strings representing the nodejs/node repo full project root with
59+
// `<project-root>`. Also replaces file URLs containing the full project root path.
60+
// The project root path may contain unicode characters.
61+
function transformProjectRoot(replacement = '<project-root>') {
3562
const projectRoot = path.resolve(__dirname, '../..');
63+
// Handles output already processed by `replaceWindowsPaths`.
64+
const winPath = replaceWindowsPaths(projectRoot);
65+
// Handles URL encoded project root in file URL strings as well.
66+
const urlEncoded = pathToFileURL(projectRoot).pathname;
3667
return (str) => {
37-
return str.replaceAll('\\\'', "'").replaceAll(projectRoot, replacement);
68+
return str.replaceAll('\\\'', "'")
69+
// Replace fileUrl first as `winPath` could be a substring of the fileUrl.
70+
.replaceAll(urlEncoded, replacement)
71+
.replaceAll(projectRoot, replacement)
72+
.replaceAll(winPath, replacement);
3873
};
3974
}
4075

@@ -152,32 +187,41 @@ function pickTestFileFromLcov(str) {
152187
);
153188
}
154189

155-
const defaultTransform = transform(
190+
// Transforms basic patterns like:
191+
// - platform specific path and line endings,
192+
// - line trailing spaces,
193+
// - executable specific path and versions.
194+
const basicTransform = transform(
156195
replaceWindowsLineEndings,
157-
replaceStackTrace,
196+
replaceTrailingSpaces,
158197
removeWindowsPathEscaping,
159-
transformProjectRoot(),
160198
replaceWindowsPaths,
199+
replaceNodeVersion,
200+
generalizeExeName,
201+
replaceWarningPid,
202+
);
203+
204+
const defaultTransform = transform(
205+
basicTransform,
206+
replaceStackTrace,
207+
transformProjectRoot(),
161208
replaceTestDuration,
162209
replaceTestLocationLine,
163210
);
164211
const specTransform = transform(
165212
replaceSpecDuration,
166-
replaceWindowsLineEndings,
213+
basicTransform,
167214
replaceStackTrace,
168-
replaceWindowsPaths,
169215
);
170216
const junitTransform = transform(
171217
replaceJunitDuration,
172-
replaceWindowsLineEndings,
218+
basicTransform,
173219
replaceStackTrace,
174-
replaceWindowsPaths,
175220
);
176221
const lcovTransform = transform(
177-
replaceWindowsLineEndings,
222+
basicTransform,
178223
replaceStackTrace,
179224
transformProjectRoot(),
180-
replaceWindowsPaths,
181225
pickTestFileFromLcov,
182226
);
183227

@@ -204,6 +248,7 @@ module.exports = {
204248
transform,
205249
transformProjectRoot,
206250
replaceTestDuration,
251+
basicTransform,
207252
defaultTransform,
208253
specTransform,
209254
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+
<project-root>/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://<project-root>/[eval1]:2:9)
3+
at two (file://<project-root>/[eval1]:15:9)
4+
at async three (file://<project-root>/[eval1]:18:3)
5+
at async four (file://<project-root>/[eval1]:22:3)
6+
at async main (file://<project-root>/[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 (<project-root>/test/fixtures/async-error.js:4:9)
3+
at two (<project-root>/test/fixtures/async-error.js:17:9)
4+
at async three (<project-root>/test/fixtures/async-error.js:20:3)
5+
at async four (<project-root>/test/fixtures/async-error.js:24:3)
6+
at async main (<project-root>/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 (<project-root>/test/fixtures/async-error.js:4:9)
3+
at two (<project-root>/test/fixtures/async-error.js:17:9)
4+
at process.processTicksAndRejections (node:internal/process/task_queues:104:5)
5+
at async three (<project-root>/test/fixtures/async-error.js:20:3)
6+
at async four (<project-root>/test/fixtures/async-error.js:24:3)
7+
at async main (<project-root>/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 (<project-root>/test/fixtures/async-error.js:4:9)
3+
at two (<project-root>/test/fixtures/async-error.js:17:9)
4+
at async three (<project-root>/test/fixtures/async-error.js:20:3)
5+
at async four (<project-root>/test/fixtures/async-error.js:24:3)
6+
at async main (file://<project-root>/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 (<project-root>/test/fixtures/async-error.js:4:9)
3+
at two (<project-root>/test/fixtures/async-error.js:17:9)
4+
at async three (<project-root>/test/fixtures/async-error.js:20:3)
5+
at async four (<project-root>/test/fixtures/async-error.js:24:3)
6+
at async main (<project-root>/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> (<project-root>/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+
<project-root>/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> (<project-root>/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> (<project-root>/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> (<project-root>/test/fixtures/errors/error_aggregateTwoErrors.js:*:*) {
1515
code: 'ERR1'
1616
}
1717
]
1818
}
1919

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

test/fixtures/errors/error_exit.snapshot

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ 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> (<project-root>/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)
0