Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 03dcf7b

Browse filesBrowse files
committed
test: migrate message tests to use assertSnapshot
PR-URL: #47498 Reviewed-By: Geoffrey Booth <webadmin@geoffreybooth.com> Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com>
1 parent dedbeee commit 03dcf7b
Copy full SHA for 03dcf7b

File tree

Expand file treeCollapse file tree

70 files changed

+274
-84
lines changed
Open diff view settings
Filter options

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.
Dismiss banner
Expand file treeCollapse file tree

70 files changed

+274
-84
lines changed
Open diff view settings
Collapse file

‎test/common/assertSnapshot.js‎

Copy file name to clipboardExpand all lines: test/common/assertSnapshot.js
+23-6Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,18 @@ const assert = require('node:assert/strict');
88
const stackFramesRegexp = /(\s+)((.+?)\s+\()?(?:\(?(.+?):(\d+)(?::(\d+))?)\)?(\s+\{)?(\n|$)/g;
99
const windowNewlineRegexp = /\r/g;
1010

11-
function replaceStackTrace(str) {
12-
return str.replace(stackFramesRegexp, '$1*$7\n');
11+
function replaceStackTrace(str, replacement = '$1*$7\n') {
12+
return str.replace(stackFramesRegexp, replacement);
1313
}
1414

1515
function replaceWindowsLineEndings(str) {
1616
return str.replace(windowNewlineRegexp, '');
1717
}
1818

19+
function replaceWindowsPaths(str) {
20+
return str.replaceAll(path.win32.sep, path.posix.sep);
21+
}
22+
1923
function transform(...args) {
2024
return (str) => args.reduce((acc, fn) => fn(acc), str);
2125
}
@@ -35,19 +39,32 @@ async function assertSnapshot(actual, filename = process.argv[1]) {
3539
}
3640
}
3741

42+
/**
43+
* Spawn a process and assert its output against a snapshot.
44+
* if you want to automatically update the snapshot, run tests with NODE_REGENERATE_SNAPSHOTS=1
45+
* transform is a function that takes the output and returns a string that will be compared against the snapshot
46+
* this is useful for normalizing output such as stack traces
47+
* there are some predefined transforms in this file such as replaceStackTrace and replaceWindowsLineEndings
48+
* both of which can be used as an example for writing your own
49+
* compose multiple transforms by passing them as arguments to the transform function:
50+
* assertSnapshot.transform(assertSnapshot.replaceStackTrace, assertSnapshot.replaceWindowsLineEndings)
51+
*
52+
* @param {string} filename
53+
* @param {function(string): string} [transform]
54+
* @returns {Promise<void>}
55+
*/
3856
async function spawnAndAssert(filename, transform = (x) => x) {
39-
// TODO: Add an option to this function to alternatively or additionally compare stderr.
40-
// For now, tests that want to check stderr or both stdout and stderr can use spawnPromisified.
4157
const flags = common.parseTestFlags(filename);
42-
const { stdout } = await common.spawnPromisified(process.execPath, [...flags, filename]);
43-
await assertSnapshot(transform(stdout), filename);
58+
const { stdout, stderr } = await common.spawnPromisified(process.execPath, [...flags, filename]);
59+
await assertSnapshot(transform(`${stdout}${stderr}`), filename);
4460
}
4561

4662
module.exports = {
4763
assertSnapshot,
4864
getSnapshotPath,
4965
replaceStackTrace,
5066
replaceWindowsLineEndings,
67+
replaceWindowsPaths,
5168
spawnAndAssert,
5269
transform,
5370
};
Collapse file

‎test/common/index.js‎

Copy file name to clipboardExpand all lines: test/common/index.js
+3-2Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ function parseTestFlags(filename = process.argv[1]) {
6969
fs.closeSync(fd);
7070
const source = buffer.toString('utf8', 0, bytesRead);
7171

72-
const flagStart = source.indexOf('// Flags: --') + 10;
72+
const flagStart = source.search(/\/\/ Flags:\s+--/) + 10;
7373

7474
if (flagStart === 9) {
7575
return [];
@@ -82,7 +82,8 @@ function parseTestFlags(filename = process.argv[1]) {
8282
return source
8383
.substring(flagStart, flagEnd)
8484
.replace(/_/g, '-')
85-
.split(' ');
85+
.split(/\s+/)
86+
.filter(Boolean);
8687
}
8788

8889
// Check for flags. Skip this for workers (both, the `cluster` module and
Collapse file
+1-1Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
// USE OR OTHER DEALINGS IN THE SOFTWARE.
2121

2222
'use strict';
23-
require('../common');
23+
require('../../common');
2424

2525
console.log([
2626
'_______________________________________________50',
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
'use strict';
22

3-
require('../common');
3+
require('../../common');
44

55
console.trace('foo');
Collapse file
+1-1Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
Trace: foo
2-
at Object.<anonymous> (*console.js:*:*)
2+
at *
33
at *
44
at *
55
at *
Collapse file

‎test/message/console_low_stack_space.js‎ ‎…tures/console/console_low_stack_space.js‎test/message/console_low_stack_space.js renamed to test/fixtures/console/console_low_stack_space.js test/message/console_low_stack_space.js renamed to test/fixtures/console/console_low_stack_space.js

Copy file name to clipboardExpand all lines: test/fixtures/console/console_low_stack_space.js
+1-1Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ Object.defineProperty(global, 'console', {
77
value: {},
88
});
99

10-
require('../common');
10+
require('../../common');
1111

1212
// This test checks that, if Node cannot put together the `console` object
1313
// because it is low on stack space while doing so, it can succeed later
Collapse file
File renamed without changes.
Collapse file
+1-1Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,6 @@
2020
// USE OR OTHER DEALINGS IN THE SOFTWARE.
2121

2222
'use strict';
23-
require('../common');
23+
require('../../common');
2424

2525
console.log('hello world');
Collapse file
File renamed without changes.

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.