Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: migrate message tests to use assertSnapshot #47498

Merged
merged 12 commits into from
Apr 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 23 additions & 6 deletions test/common/assertSnapshot.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,18 @@ const assert = require('node:assert/strict');
const stackFramesRegexp = /(\s+)((.+?)\s+\()?(?:\(?(.+?):(\d+)(?::(\d+))?)\)?(\s+\{)?(\n|$)/g;
const windowNewlineRegexp = /\r/g;

function replaceStackTrace(str) {
return str.replace(stackFramesRegexp, '$1*$7\n');
function replaceStackTrace(str, replacement = '$1*$7\n') {
return str.replace(stackFramesRegexp, replacement);
}

function replaceWindowsLineEndings(str) {
return str.replace(windowNewlineRegexp, '');
}

function replaceWindowsPaths(str) {
return str.replaceAll(path.win32.sep, path.posix.sep);
}

function transform(...args) {
return (str) => args.reduce((acc, fn) => fn(acc), str);
}
Expand All @@ -35,19 +39,32 @@ async function assertSnapshot(actual, filename = process.argv[1]) {
}
}

/**
* Spawn a process and assert its output against a snapshot.
* if you want to automatically update the snapshot, run tests with NODE_REGENERATE_SNAPSHOTS=1
* transform is a function that takes the output and returns a string that will be compared against the snapshot
* this is useful for normalizing output such as stack traces
* there are some predefined transforms in this file such as replaceStackTrace and replaceWindowsLineEndings
* both of which can be used as an example for writing your own
* compose multiple transforms by passing them as arguments to the transform function:
* assertSnapshot.transform(assertSnapshot.replaceStackTrace, assertSnapshot.replaceWindowsLineEndings)
*
* @param {string} filename
* @param {function(string): string} [transform]
* @returns {Promise<void>}
*/
async function spawnAndAssert(filename, transform = (x) => x) {
MoLow marked this conversation as resolved.
Show resolved Hide resolved
// TODO: Add an option to this function to alternatively or additionally compare stderr.
// For now, tests that want to check stderr or both stdout and stderr can use spawnPromisified.
const flags = common.parseTestFlags(filename);
const { stdout } = await common.spawnPromisified(process.execPath, [...flags, filename]);
await assertSnapshot(transform(stdout), filename);
const { stdout, stderr } = await common.spawnPromisified(process.execPath, [...flags, filename]);
await assertSnapshot(transform(`${stdout}${stderr}`), filename);
GeoffreyBooth marked this conversation as resolved.
Show resolved Hide resolved
}

module.exports = {
assertSnapshot,
getSnapshotPath,
replaceStackTrace,
replaceWindowsLineEndings,
replaceWindowsPaths,
spawnAndAssert,
transform,
};
5 changes: 3 additions & 2 deletions test/common/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ function parseTestFlags(filename = process.argv[1]) {
fs.closeSync(fd);
const source = buffer.toString('utf8', 0, bytesRead);

const flagStart = source.indexOf('// Flags: --') + 10;
const flagStart = source.search(/\/\/ Flags:\s+--/) + 10;
MoLow marked this conversation as resolved.
Show resolved Hide resolved

if (flagStart === 9) {
return [];
Expand All @@ -83,7 +83,8 @@ function parseTestFlags(filename = process.argv[1]) {
return source
.substring(flagStart, flagEnd)
.replace(/_/g, '-')
.split(' ');
.split(/\s+/)
.filter(Boolean);
MoLow marked this conversation as resolved.
Show resolved Hide resolved
}

// Check for flags. Skip this for workers (both, the `cluster` module and
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
// USE OR OTHER DEALINGS IN THE SOFTWARE.

'use strict';
require('../common');
require('../../common');

console.log([
'_______________________________________________50',
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
'use strict';

require('../common');
require('../../common');

console.trace('foo');
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
Trace: foo
at Object.<anonymous> (*console.js:*:*)
at *
at *
at *
at *
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ Object.defineProperty(global, 'console', {
value: {},
});

require('../common');
require('../../common');

// This test checks that, if Node cannot put together the `console` object
// because it is low on stack space while doing so, it can succeed later
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,6 @@
// USE OR OTHER DEALINGS IN THE SOFTWARE.

'use strict';
require('../common');
require('../../common');

console.log('hello world');
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
// USE OR OTHER DEALINGS IN THE SOFTWARE.

'use strict';
require('../common');
require('../../common');

Error.stackTraceLimit = 0;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
before
*test*message*stack_overflow.js:*
*test*fixtures*console*stack_overflow.js:*
JSON.stringify(array);
^

Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
'use strict';

require('../common');
require('../../common');
const { spawnSync } = require('child_process');

const four = require('../common/fixtures')
const four = require('../../common/fixtures')
.readSync('async-error.js')
.toString()
.split('\n')
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@ Error: test
at two ([eval]:15:9)
at async three ([eval]:18:3)
at async four ([eval]:22:3)
at async main ([eval]:28:5)
at async main ([eval]:28:5)

Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
'use strict';

require('../common');
require('../../common');
const { spawnSync } = require('child_process');

const four = require('../common/fixtures')
const four = require('../../common/fixtures')
.readSync('async-error.js')
.toString()
.split('\n')
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ Error: test
at async three (file:*/[eval1]:18:3)
at async four (file:*/[eval1]:22:3)
at async main (file:*/[eval1]:28:5)

Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use strict';
require('../common');
const four = require('../fixtures/async-error');
require('../../common');
const four = require('../async-error');

async function main() {
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@ Error: test
at two (*fixtures*async-error.js:17:9)
at async three (*fixtures*async-error.js:20:3)
at async four (*fixtures*async-error.js:24:3)
at async main (*message*async_error_sync_esm.mjs:6:5)
at async main (*async_error_microtask_main.js:7:5)
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use strict';
require('../common');
const four = require('../fixtures/async-error');
require('../../common');
const four = require('../async-error');

async function main() {
try {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Error: test
at one (*fixtures*async-error.js:4:9)
at two (*fixtures*async-error.js:17:9)
at process.processTicksAndRejections (node:internal/process/task_queues:*:*)
at process.processTicksAndRejections (node:internal*process*task_queues:95:5)
at async three (*fixtures*async-error.js:20:3)
at async four (*fixtures*async-error.js:24:3)
at async main (*message*async_error_nexttick_main.js:7:5)
at async main (*async_error_nexttick_main.js:7:5)
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import '../common/index.mjs';
import four from '../fixtures/async-error.js';
import '../../common/index.mjs';
import four from '../async-error.js';

async function main() {
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@ Error: test
at two (*fixtures*async-error.js:17:9)
at async three (*fixtures*async-error.js:20:3)
at async four (*fixtures*async-error.js:24:3)
at async main (*message*async_error_sync_main.js:7:5)
at async main (file:*/async_error_sync_esm.mjs:6:5)
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use strict';
require('../common');
const four = require('../fixtures/async-error');
require('../../common');
const four = require('../async-error');

async function main() {
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@ Error: test
at two (*fixtures*async-error.js:17:9)
at async three (*fixtures*async-error.js:20:3)
at async four (*fixtures*async-error.js:24:3)
at async main (*message*async_error_microtask_main.js:7:5)
at async main (*async_error_sync_main.js:7:5)
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Flags: --expose-internals
'use strict';

require('../common');
require('../../common');
Error.stackTraceLimit = 1;

const { aggregateTwoErrors } = require('internal/errors');
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
*error_aggregateTwoErrors.js:*
throw aggregateTwoErrors(err, originalError);
^

[AggregateError: original] {
code: 'ERR0',
[errors]: [
Error: original
at Object.<anonymous> (*test*message*error_aggregateTwoErrors.js:*:*) {
at Object.<anonymous> (*error_aggregateTwoErrors.js:*:*) {
code: 'ERR0'
},
Error: second error
at Object.<anonymous> (*test*message*error_aggregateTwoErrors.js:*:*) {
at Object.<anonymous> (*error_aggregateTwoErrors.js:*:*) {
code: 'ERR1'
}
]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
// USE OR OTHER DEALINGS IN THE SOFTWARE.

'use strict';
require('../common');
require('../../common');
Error.stackTraceLimit = 1;

const assert = require('assert');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ AssertionError [ERR_ASSERTION]: Expected values to be strictly equal:

1 !== 2

at Object.<anonymous> (*test*message*error_exit.js:*:*) {
at Object.<anonymous> (*error_exit.js:*:*) {
generatedMessage: true,
code: 'ERR_ASSERTION',
actual: 1,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
'use strict';
require('../common');
require('../../common');
Error.stackTraceLimit = 2;

function test() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
'use strict';
require('../common');
require('../../common');
Error.stackTraceLimit = 2;

const EventEmitter = require('events');
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
node:events:*
throw er; // Unhandled 'error' event
throw er; * Unhandled 'error' event
^

Error: foo:bar
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
'use strict';
require('../common');
require('../../common');
Error.stackTraceLimit = 1;

const EventEmitter = require('events');
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
node:events:*
throw er; // Unhandled 'error' event
throw er; * Unhandled 'error' event
^

Error
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
'use strict';
require('../common');
require('../../common');
Error.stackTraceLimit = 1;

const EventEmitter = require('events');
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
node:events:*
throw er; // Unhandled 'error' event
throw er; * Unhandled 'error' event
^

Error
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
'use strict';
require('../common');
require('../../common');
Error.stackTraceLimit = 1;

const EventEmitter = require('events');
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
node:events:*
throw er; // Unhandled 'error' event
throw er; * Unhandled 'error' event
^

Error
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Flags: --unhandled-rejections=strict
'use strict';

require('../common');
require('../../common');

// Check that the process will exit on the first unhandled rejection in case the
// unhandled rejections mode is set to `'strict'`.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
*promise_always_throw_unhandled.js:*
throw new Error('One');
^

Error: One
at *promise_always_throw_unhandled.js:*:*
at *
at new Promise (<anonymous>)
at Object.<anonymous> (*promise_always_throw_unhandled.js:*:*)
at *
at *
at *
at *
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
// USE OR OTHER DEALINGS IN THE SOFTWARE.

'use strict';
require('../common');
require('../../common');

// Custom error throwing
// eslint-disable-next-line no-throw-literal
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
*test*message*throw_custom_error.js:*

*throw_custom_error.js:*
throw ({ name: 'MyCustomError', message: 'This is a custom message' });
^
{ name: 'MyCustomError', message: 'This is a custom message' }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@

/* eslint-disable indent, no-tabs */
'use strict';
require('../common');
require('../../common');

console.error('before');

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
before
*test*message*throw_in_line_with_tabs.js:*

*throw_in_line_with_tabs.js:*
throw ({ foo: 'bar' });
^
{ foo: 'bar' }
Expand Down
Loading