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 10, 2023
1 parent 57508fb commit a6a3b08
Show file tree
Hide file tree
Showing 83 changed files with 267 additions and 152 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,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
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,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);
}
File renamed without changes.
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);
}
File renamed without changes.
7 changes: 7 additions & 0 deletions test/fixtures/message/source_map_enclosing_function.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
// Flags: --enable-source-maps

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

require('../../fixtures/source-map/enclosing-call-site-min.js');
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
throw err
^


Error: an error!
at functionD (*enclosing-call-site.js:16:17)
at functionC (*enclosing-call-site.js:10:3)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
// Flags: --enable-source-maps

'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

0 comments on commit a6a3b08

Please sign in to comment.