Skip to content

Commit

Permalink
test: migrate message tests to use assertSnapshot
Browse files Browse the repository at this point in the history
  • Loading branch information
MoLow committed Apr 14, 2023
1 parent c94be41 commit 55fffdb
Show file tree
Hide file tree
Showing 100 changed files with 333 additions and 184 deletions.
10 changes: 4 additions & 6 deletions test/common/assertSnapshot.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ 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) {
Expand All @@ -36,11 +36,9 @@ async function assertSnapshot(actual, filename = process.argv[1]) {
}

async function spawnAndAssert(filename, transform = (x) => x) {
// 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);
}

module.exports = {
Expand Down
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;

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

// 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,6 +1,6 @@
'use strict';

require('../common');
require('../../common');
const assert = require('assert').strict;

assert.throws(() => { throw new Error('foo'); }, { bar: true });
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ AssertionError [ERR_ASSERTION]: Expected values to be strictly deep-equal:
- Comparison {
- bar: true
- }
at Object.<anonymous> (*assert_throws_stack.js:*:*)
at *
at *
at *
at *
Expand All @@ -19,16 +19,16 @@ AssertionError [ERR_ASSERTION]: Expected values to be strictly deep-equal:
generatedMessage: true,
code: 'ERR_ASSERTION',
actual: Error: foo
at assert.throws.bar (*assert_throws_stack.js:*)
at getActual (node:assert:*)
at Function.throws (node:assert:*)
at Object.<anonymous> (*assert_throws_stack.js:*:*)
at *
at *
at *
at *
at *
at *,
at *
at *
at *
at *
at node:internal*main*run_main_module:*:*,
expected: { bar: true },
operator: 'throws'
}
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 (*fixtures*message*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 (*fixtures*message*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:**fixtures*message*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 (*fixtures*message*async_error_sync_main.js:7:5)
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
File renamed without changes.
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
File renamed without changes.
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
Expand Up @@ -20,6 +20,6 @@
// USE OR OTHER DEALINGS IN THE SOFTWARE.

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

console.log('hello world');
File renamed without changes.
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
@@ -1,7 +1,7 @@
// Flags: --unhandled-rejections=warn-with-error-code
'use strict';

require('../common');
require('../../common');
const assert = require('assert');

Promise.reject(new Error('alas'));
Expand Down
10 changes: 10 additions & 0 deletions test/fixtures/message/promise_unhandled_warn_with_error.snapshot
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
(node:*) UnhandledPromiseRejectionWarning: Error: alas
at *
at *
at *
at *
at *
at *
at *
(Use `node --trace-warnings ...` to show where the warning was created)
(node:*) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https:*nodejs.org*api*cli.html#cli_unhandled_rejections_mode). (rejection id: 1)
Loading

0 comments on commit 55fffdb

Please sign in to comment.