Skip to content

Commit 03dcf7b

Browse 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

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

+23-6
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

+3-2
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

test/message/2100bytes.js test/fixtures/console/2100bytes.js

+1-1
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',
File renamed without changes.
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 test/fixtures/console/console.snapshot

+1-1
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 test/fixtures/console/console_low_stack_space.js

+1-1
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 test/fixtures/console/hello_world.js

+1-1
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');
File renamed without changes.

test/message/stack_overflow.js test/fixtures/console/stack_overflow.js

+1-1
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
Error.stackTraceLimit = 0;
2626

test/message/stack_overflow.out test/fixtures/console/stack_overflow.snapshot

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
before
2-
*test*message*stack_overflow.js:*
2+
*test*fixtures*console*stack_overflow.js:*
33
JSON.stringify(array);
44
^
55

test/message/async_error_eval_cjs.js test/fixtures/errors/async_error_eval_cjs.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
'use strict';
22

3-
require('../common');
3+
require('../../common');
44
const { spawnSync } = require('child_process');
55

6-
const four = require('../common/fixtures')
6+
const four = require('../../common/fixtures')
77
.readSync('async-error.js')
88
.toString()
99
.split('\n')

test/message/async_error_eval_cjs.out test/fixtures/errors/async_error_eval_cjs.snapshot

+2-1
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,5 @@ Error: test
33
at two ([eval]:15:9)
44
at async three ([eval]:18:3)
55
at async four ([eval]:22:3)
6-
at async main ([eval]:28:5)
6+
at async main ([eval]:28:5)
7+

test/message/async_error_eval_esm.js test/fixtures/errors/async_error_eval_esm.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
'use strict';
22

3-
require('../common');
3+
require('../../common');
44
const { spawnSync } = require('child_process');
55

6-
const four = require('../common/fixtures')
6+
const four = require('../../common/fixtures')
77
.readSync('async-error.js')
88
.toString()
99
.split('\n')

test/message/async_error_eval_esm.out test/fixtures/errors/async_error_eval_esm.snapshot

+1
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,4 @@ Error: test
44
at async three (file:*/[eval1]:18:3)
55
at async four (file:*/[eval1]:22:3)
66
at async main (file:*/[eval1]:28:5)
7+

test/message/async_error_microtask_main.js test/fixtures/errors/async_error_microtask_main.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
'use strict';
2-
require('../common');
3-
const four = require('../fixtures/async-error');
2+
require('../../common');
3+
const four = require('../async-error');
44

55
async function main() {
66
try {

test/message/async_error_sync_esm.out test/fixtures/errors/async_error_microtask_main.snapshot

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,4 @@ Error: test
33
at two (*fixtures*async-error.js:17:9)
44
at async three (*fixtures*async-error.js:20:3)
55
at async four (*fixtures*async-error.js:24:3)
6-
at async main (*message*async_error_sync_esm.mjs:6:5)
6+
at async main (*async_error_microtask_main.js:7:5)

test/message/async_error_nexttick_main.js test/fixtures/errors/async_error_nexttick_main.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
'use strict';
2-
require('../common');
3-
const four = require('../fixtures/async-error');
2+
require('../../common');
3+
const four = require('../async-error');
44

55
async function main() {
66
try {
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
Error: test
22
at one (*fixtures*async-error.js:4:9)
33
at two (*fixtures*async-error.js:17:9)
4-
at process.processTicksAndRejections (node:internal/process/task_queues:*:*)
4+
at process.processTicksAndRejections (node:internal*process*task_queues:95:5)
55
at async three (*fixtures*async-error.js:20:3)
66
at async four (*fixtures*async-error.js:24:3)
7-
at async main (*message*async_error_nexttick_main.js:7:5)
7+
at async main (*async_error_nexttick_main.js:7:5)

test/message/async_error_sync_esm.mjs test/fixtures/errors/async_error_sync_esm.mjs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import '../common/index.mjs';
2-
import four from '../fixtures/async-error.js';
1+
import '../../common/index.mjs';
2+
import four from '../async-error.js';
33

44
async function main() {
55
try {

test/message/async_error_sync_main.out test/fixtures/errors/async_error_sync_esm.snapshot

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,4 @@ Error: test
33
at two (*fixtures*async-error.js:17:9)
44
at async three (*fixtures*async-error.js:20:3)
55
at async four (*fixtures*async-error.js:24:3)
6-
at async main (*message*async_error_sync_main.js:7:5)
6+
at async main (file:*/async_error_sync_esm.mjs:6:5)

test/message/async_error_sync_main.js test/fixtures/errors/async_error_sync_main.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
'use strict';
2-
require('../common');
3-
const four = require('../fixtures/async-error');
2+
require('../../common');
3+
const four = require('../async-error');
44

55
async function main() {
66
try {

test/message/async_error_microtask_main.out test/fixtures/errors/async_error_sync_main.snapshot

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,4 @@ Error: test
33
at two (*fixtures*async-error.js:17:9)
44
at async three (*fixtures*async-error.js:20:3)
55
at async four (*fixtures*async-error.js:24:3)
6-
at async main (*message*async_error_microtask_main.js:7:5)
6+
at async main (*async_error_sync_main.js:7:5)

test/message/error_aggregateTwoErrors.js test/fixtures/errors/error_aggregateTwoErrors.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// Flags: --expose-internals
22
'use strict';
33

4-
require('../common');
4+
require('../../common');
55
Error.stackTraceLimit = 1;
66

77
const { aggregateTwoErrors } = require('internal/errors');

test/message/error_aggregateTwoErrors.out test/fixtures/errors/error_aggregateTwoErrors.snapshot

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,16 @@
11
*error_aggregateTwoErrors.js:*
22
throw aggregateTwoErrors(err, originalError);
33
^
4+
45
[AggregateError: original] {
56
code: 'ERR0',
67
[errors]: [
78
Error: original
8-
at Object.<anonymous> (*test*message*error_aggregateTwoErrors.js:*:*) {
9+
at Object.<anonymous> (*error_aggregateTwoErrors.js:*:*) {
910
code: 'ERR0'
1011
},
1112
Error: second error
12-
at Object.<anonymous> (*test*message*error_aggregateTwoErrors.js:*:*) {
13+
at Object.<anonymous> (*error_aggregateTwoErrors.js:*:*) {
1314
code: 'ERR1'
1415
}
1516
]

test/message/error_exit.js test/fixtures/errors/error_exit.js

+1-1
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
Error.stackTraceLimit = 1;
2525

2626
const assert = require('assert');

test/message/error_exit.out test/fixtures/errors/error_exit.snapshot

+1-1
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> (*test*message*error_exit.js:*:*) {
10+
at Object.<anonymous> (*error_exit.js:*:*) {
1111
generatedMessage: true,
1212
code: 'ERR_ASSERTION',
1313
actual: 1,

test/message/error_with_nul.js test/fixtures/errors/error_with_nul.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
'use strict';
2-
require('../common');
2+
require('../../common');
33
Error.stackTraceLimit = 2;
44

55
function test() {
File renamed without changes.

test/message/events_unhandled_error_common_trace.js test/fixtures/errors/events_unhandled_error_common_trace.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
'use strict';
2-
require('../common');
2+
require('../../common');
33
Error.stackTraceLimit = 2;
44

55
const EventEmitter = require('events');

test/message/events_unhandled_error_common_trace.out test/fixtures/errors/events_unhandled_error_common_trace.snapshot

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
node:events:*
2-
throw er; // Unhandled 'error' event
2+
throw er; * Unhandled 'error' event
33
^
44

55
Error: foo:bar

test/message/events_unhandled_error_nexttick.js test/fixtures/errors/events_unhandled_error_nexttick.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
'use strict';
2-
require('../common');
2+
require('../../common');
33
Error.stackTraceLimit = 1;
44

55
const EventEmitter = require('events');

test/message/events_unhandled_error_nexttick.out test/fixtures/errors/events_unhandled_error_nexttick.snapshot

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
node:events:*
2-
throw er; // Unhandled 'error' event
2+
throw er; * Unhandled 'error' event
33
^
44

55
Error

test/message/events_unhandled_error_sameline.js test/fixtures/errors/events_unhandled_error_sameline.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
'use strict';
2-
require('../common');
2+
require('../../common');
33
Error.stackTraceLimit = 1;
44

55
const EventEmitter = require('events');

test/message/events_unhandled_error_sameline.out test/fixtures/errors/events_unhandled_error_sameline.snapshot

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
node:events:*
2-
throw er; // Unhandled 'error' event
2+
throw er; * Unhandled 'error' event
33
^
44

55
Error

test/message/events_unhandled_error_subclass.js test/fixtures/errors/events_unhandled_error_subclass.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
'use strict';
2-
require('../common');
2+
require('../../common');
33
Error.stackTraceLimit = 1;
44

55
const EventEmitter = require('events');

test/message/events_unhandled_error_subclass.out test/fixtures/errors/events_unhandled_error_subclass.snapshot

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
node:events:*
2-
throw er; // Unhandled 'error' event
2+
throw er; * Unhandled 'error' event
33
^
44

55
Error

test/message/promise_always_throw_unhandled.js test/fixtures/errors/promise_always_throw_unhandled.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// Flags: --unhandled-rejections=strict
22
'use strict';
33

4-
require('../common');
4+
require('../../common');
55

66
// Check that the process will exit on the first unhandled rejection in case the
77
// unhandled rejections mode is set to `'strict'`.

test/message/promise_always_throw_unhandled.out test/fixtures/errors/promise_always_throw_unhandled.snapshot

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
*promise_always_throw_unhandled.js:*
22
throw new Error('One');
33
^
4+
45
Error: One
5-
at *promise_always_throw_unhandled.js:*:*
6+
at *
67
at new Promise (<anonymous>)
7-
at Object.<anonymous> (*promise_always_throw_unhandled.js:*:*)
8+
at *
89
at *
910
at *
1011
at *

test/message/throw_custom_error.js test/fixtures/errors/throw_custom_error.js

+1-1
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
// Custom error throwing
2626
// eslint-disable-next-line no-throw-literal

test/message/throw_custom_error.out test/fixtures/errors/throw_custom_error.snapshot

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
*test*message*throw_custom_error.js:*
1+
2+
*throw_custom_error.js:*
23
throw ({ name: 'MyCustomError', message: 'This is a custom message' });
34
^
45
{ name: 'MyCustomError', message: 'This is a custom message' }

test/message/throw_in_line_with_tabs.js test/fixtures/errors/throw_in_line_with_tabs.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121

2222
/* eslint-disable indent, no-tabs */
2323
'use strict';
24-
require('../common');
24+
require('../../common');
2525

2626
console.error('before');
2727

test/message/throw_in_line_with_tabs.out test/fixtures/errors/throw_in_line_with_tabs.snapshot

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
before
2-
*test*message*throw_in_line_with_tabs.js:*
2+
3+
*throw_in_line_with_tabs.js:*
34
throw ({ foo: 'bar' });
45
^
56
{ foo: 'bar' }

0 commit comments

Comments
 (0)