GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — test: migrate message tests to use assertSnapshot · nodejs/node@34bfb69

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • test
    • common
      • assertSnapshot.js
      • index.js
    • fixtures
      • console
        • 2100bytes.js
        • 2100bytes.snapshot
        • console.js
        • console.snapshot
        • console_low_stack_space.js
        • console_low_stack_space.snapshot
        • hello_world.js
        • hello_world.snapshot
        • stack_overflow.js
        • stack_overflow.snapshot
      • errors
        • async_error_eval_cjs.js
        • async_error_eval_cjs.snapshot
        • async_error_eval_esm.js
        • async_error_eval_esm.snapshot
        • async_error_microtask_main.js
        • async_error_microtask_main.snapshot
        • async_error_nexttick_main.js
        • async_error_nexttick_main.snapshot
        • async_error_sync_esm.mjs
        • async_error_sync_esm.snapshot
        • async_error_sync_main.js
        • async_error_sync_main.snapshot
        • error_aggregateTwoErrors.js
        • error_aggregateTwoErrors.snapshot
        • error_exit.js
        • error_exit.snapshot
        • error_with_nul.js
        • error_with_nul.snapshot
        • events_unhandled_error_common_trace.js
        • events_unhandled_error_common_trace.snapshot
        • events_unhandled_error_nexttick.js
        • events_unhandled_error_nexttick.snapshot
        • events_unhandled_error_sameline.js
        • events_unhandled_error_sameline.snapshot
        • events_unhandled_error_subclass.js
        • events_unhandled_error_subclass.snapshot
        • promise_always_throw_unhandled.js
        • promise_always_throw_unhandled.snapshot
        • throw_custom_error.js
        • throw_custom_error.snapshot
        • throw_in_line_with_tabs.js
        • throw_in_line_with_tabs.snapshot
        • throw_non_error.js
        • throw_non_error.snapshot
      • source-map/output
        • source_map_disabled_by_api.js
        • source_map_disabled_by_api.snapshot
        • source_map_enabled_by_api.js
        • source_map_enabled_by_api.snapshot
        • source_map_eval.js
        • source_map_eval.snapshot
        • source_map_no_source_file.js
        • source_map_no_source_file.snapshot
        • source_map_throw_first_tick.js
        • source_map_throw_first_tick.snapshot
      • vm
        • vm_caught_custom_runtime_error.js
        • vm_caught_custom_runtime_error.snapshot
        • vm_display_runtime_error.js
        • vm_display_runtime_error.snapshot
        • vm_display_syntax_error.js
        • vm_display_syntax_error.snapshot
        • vm_dont_display_runtime_error.js
        • vm_dont_display_runtime_error.snapshot
        • vm_dont_display_syntax_error.js
        • vm_dont_display_syntax_error.snapshot
    • parallel
      • test-node-output-console.mjs
      • test-node-output-errors.mjs
      • test-node-output-sourcemaps.mjs
      • test-node-output-vm.mjs

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
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
@@ -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
Collapse file

‎test/message/2100bytes.js‎ ‎test/fixtures/console/2100bytes.js‎test/message/2100bytes.js renamed to test/fixtures/console/2100bytes.js

Copy file name to clipboardExpand all lines: test/fixtures/console/2100bytes.js
+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',
Collapse file

‎test/message/2100bytes.out‎ ‎test/fixtures/console/2100bytes.snapshot‎test/message/2100bytes.out renamed to test/fixtures/console/2100bytes.snapshot

Copy file name to clipboard
File renamed without changes.
Collapse file

‎test/message/console.js‎ ‎test/fixtures/console/console.js‎test/message/console.js renamed to test/fixtures/console/console.js

Copy file name to clipboard
+1-1Lines 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');
Collapse file

‎test/message/console.out‎ ‎test/fixtures/console/console.snapshot‎test/message/console.out renamed to test/fixtures/console/console.snapshot

Copy file name to clipboardExpand all lines: test/fixtures/console/console.snapshot
+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

‎test/message/console_low_stack_space.out‎ ‎…console/console_low_stack_space.snapshot‎test/message/console_low_stack_space.out renamed to test/fixtures/console/console_low_stack_space.snapshot test/message/console_low_stack_space.out renamed to test/fixtures/console/console_low_stack_space.snapshot

Copy file name to clipboard
File renamed without changes.
Collapse file

‎test/message/hello_world.js‎ ‎test/fixtures/console/hello_world.js‎test/message/hello_world.js renamed to test/fixtures/console/hello_world.js

Copy file name to clipboardExpand all lines: test/fixtures/console/hello_world.js
+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

‎test/message/hello_world.out‎ ‎…st/fixtures/console/hello_world.snapshot‎test/message/hello_world.out renamed to test/fixtures/console/hello_world.snapshot test/message/hello_world.out renamed to test/fixtures/console/hello_world.snapshot

Copy file name to clipboard
File renamed without changes.

0 commit comments

Comments
0 (0)
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.