Skip to content

Commit b6738c1

Browse files
authored
test: migrate message tests to use assertSnapshot
PR-URL: #47498 Reviewed-By: Geoffrey Booth <[email protected]> Reviewed-By: Benjamin Gruenbaum <[email protected]>
1 parent 8b66dc6 commit b6738c1

File tree

70 files changed

+274
-84
lines changed

Some content is hidden

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

70 files changed

+274
-84
lines changed

β€Žtest/common/assertSnapshot.js

Lines 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
};

β€Žtest/common/index.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ function parseTestFlags(filename = process.argv[1]) {
7070
fs.closeSync(fd);
7171
const source = buffer.toString('utf8', 0, bytesRead);
7272

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

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

8990
// Check for flags. Skip this for workers (both, the `cluster` module and

β€Žtest/message/2100bytes.js renamed to β€Žtest/fixtures/console/2100bytes.js

Lines 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',
Lines changed: 1 addition & 1 deletion
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');

β€Žtest/message/console.out renamed to β€Žtest/fixtures/console/console.snapshot

Lines 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 *

β€Žtest/message/console_low_stack_space.js renamed to β€Žtest/fixtures/console/console_low_stack_space.js

Lines 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

β€Žtest/message/hello_world.js renamed to β€Žtest/fixtures/console/hello_world.js

Lines 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');

0 commit comments

Comments
Β (0)