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 1 commit
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
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) {
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 = {
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;
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,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_main.js:7: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_microtask_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_sync_esm.mjs:6: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
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
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
@@ -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,25 +1,25 @@
// Flags: --enable-source-maps

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

process.setSourceMapsEnabled(false);

try {
require('../fixtures/source-map/enclosing-call-site-min.js');
require('../source-map/enclosing-call-site-min.js');
} catch (e) {
console.log(e);
}

delete require.cache[require
.resolve('../fixtures/source-map/enclosing-call-site-min.js')];
.resolve('../source-map/enclosing-call-site-min.js')];

// Re-enable.
process.setSourceMapsEnabled(true);

try {
require('../fixtures/source-map/enclosing-call-site-min.js');
require('../source-map/enclosing-call-site-min.js');
} catch (e) {
console.log(e);
}
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
'use strict';
require('../common');
require('../../common');
Error.stackTraceLimit = 5;

process.setSourceMapsEnabled(true);

try {
require('../fixtures/source-map/enclosing-call-site-min.js');
require('../source-map/enclosing-call-site-min.js');
} catch (e) {
console.log(e);
}

delete require.cache[require
.resolve('../fixtures/source-map/enclosing-call-site-min.js')];
.resolve('../source-map/enclosing-call-site-min.js')];

process.setSourceMapsEnabled(false);

try {
require('../fixtures/source-map/enclosing-call-site-min.js');
require('../source-map/enclosing-call-site-min.js');
} catch (e) {
console.log(e);
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
// Flags: --enable-source-maps
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How is this a fixture? This file looks like it was intended to be run as the test itself.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

since we never cared about the exit code or anything besides stdout and stderr of running this file


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

const fs = require('fs');

const content = fs.readFileSync(require.resolve('../fixtures/source-map/tabs.js'), 'utf8');
const content = fs.readFileSync(require.resolve('../source-map/tabs.js'), 'utf8');
eval(content);
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@
alert "I knew it!"
^


ReferenceError: alert is not defined
at Object.eval (*tabs.coffee:26:2)
at eval (*tabs.coffee:1:14)
at Object.<anonymous> (*source_map_eval.js:*:*)
at Object.<anonymous> (*fixtures*message*source_map_eval.js:10:1)

Node.js *
Loading