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

70 files changed

Lines changed: 274 additions & 84 deletions

File tree

Some content is hidden

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

‎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
conststackFramesRegexp=/(\s+)((.+?)\s+\()?(?:\(?(.+?):(\d+)(?::(\d+))?)\)?(\s+\{)?(\n|$)/g;
99
constwindowNewlineRegexp=/\r/g;
1010

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

1515
functionreplaceWindowsLineEndings(str){
1616
returnstr.replace(windowNewlineRegexp,'');
1717
}
1818

19+
functionreplaceWindowsPaths(str){
20+
returnstr.replaceAll(path.win32.sep,path.posix.sep);
21+
}
22+
1923
functiontransform(...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
asyncfunctionspawnAndAssert(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
constflags=common.parseTestFlags(filename);
42-
const{ stdout }=awaitcommon.spawnPromisified(process.execPath,[...flags,filename]);
43-
awaitassertSnapshot(transform(stdout),filename);
58+
const{ stdout, stderr}=awaitcommon.spawnPromisified(process.execPath,[...flags,filename]);
59+
awaitassertSnapshot(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
@@ -69,7 +69,7 @@ function parseTestFlags(filename = process.argv[1]) {
6969
fs.closeSync(fd);
7070
constsource=buffer.toString('utf8',0,bytesRead);
7171

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

7474
if(flagStart===9){
7575
return[];
@@ -82,7 +82,8 @@ function parseTestFlags(filename = process.argv[1]) {
8282
returnsource
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
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',
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');
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
File renamed without changes.
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');
File renamed without changes.

0 commit comments

Comments
 (0)