diff --git a/test/fixtures/util/util-inspect-error-cause.js b/test/fixtures/util/util-inspect-error-cause.js new file mode 100644 index 00000000000000..9cbd7d7c8e3734 --- /dev/null +++ b/test/fixtures/util/util-inspect-error-cause.js @@ -0,0 +1,33 @@ +'use strict'; + +require('../../common'); + +const { inspect } = require('util'); + +class FoobarError extends Error { + status = 'Feeling good'; +} + +const cause1 = new TypeError('Inner error'); +const cause2 = new FoobarError('Individual message', { cause: cause1 }); +cause2.extraProperties = 'Yes!'; +const cause3 = new Error('Stack causes', { cause: cause2 }); + +const cause4 = new Error('Number error cause', { cause: 42 }); +const cause5 = new Error('Object cause', { + cause: { + message: 'Unique', + name: 'Error', + stack: 'Error: Unique\n' + + ' at Module._compile (node:internal/modules/cjs/loader:827:30)', + }, +}); +const cause6 = new Error('undefined cause', { + cause: undefined, +}); + +console.log(cause4); +console.log(cause5); +console.log(cause6); +console.log(cause3); +console.log(inspect(cause3, { compact: false, breakLength: Infinity })); diff --git a/test/fixtures/util/util-inspect-error-cause.snapshot b/test/fixtures/util/util-inspect-error-cause.snapshot new file mode 100644 index 00000000000000..1c28008a3affd9 --- /dev/null +++ b/test/fixtures/util/util-inspect-error-cause.snapshot @@ -0,0 +1,69 @@ +Error: Number error cause + at * + at * + at * + at * + at * + at * + at * + at * { + [cause]: 42 +} +Error: Object cause + at * + at * + at * + at * + at * + at * + at * + at * { + [cause]: { + message: 'Unique', + name: 'Error', + stack: 'Error: Unique\n' + + ' at Module._compile (node:internal*modules*cjs*loader:*:*)' + } +} +Error: undefined cause + at * + at * + at * + at * + at * + at * + at * + at * { + [cause]: undefined +} +Error: Stack causes + at * + at * + at * + at * + at * + at * + at * + at * { + [cause]: FoobarError: Individual message + at * + at * + at * + at * + at * + at * + at * + at * { + status: 'Feeling good', + extraProperties: 'Yes!', + [cause]: TypeError: Inner error + at * + at * + at * + at * + at * + at * + at * + at * + } +} diff --git a/test/message/util_inspect_error.js b/test/fixtures/util/util_inspect_error.js similarity index 92% rename from test/message/util_inspect_error.js rename to test/fixtures/util/util_inspect_error.js index 20affd6c711fd8..ddec5e01a6ed16 100644 --- a/test/message/util_inspect_error.js +++ b/test/fixtures/util/util_inspect_error.js @@ -1,6 +1,6 @@ 'use strict'; -require('../common'); +require('../../common'); const util = require('util'); const err = new Error('foo\nbar'); diff --git a/test/message/util_inspect_error.out b/test/fixtures/util/util_inspect_error.snapshot similarity index 70% rename from test/message/util_inspect_error.out rename to test/fixtures/util/util_inspect_error.snapshot index 31b65eb2e2bf3c..19bc8c351e04cd 100644 --- a/test/message/util_inspect_error.out +++ b/test/fixtures/util/util_inspect_error.snapshot @@ -11,16 +11,16 @@ at * nested: { err: - Error: foo - bar - at *util_inspect_error* - at * - at * - at * - at * - at * - at * - at * + Error: foo + bar + at *util_inspect_error* + at * + at * + at * + at * + at * + at * + at * } } { err: Error: foo bar @@ -31,7 +31,7 @@ at * at * at * - at * + at *, nested: { err: Error: foo bar @@ -45,7 +45,7 @@ at * } } -{ Error: foo +Error: foo bar at *util_inspect_error* at * @@ -54,5 +54,6 @@ bar at * at * at * - at * - foo: 'bar' } + at * { + foo: 'bar' +} diff --git a/test/message/util-inspect-error-cause.js b/test/message/util-inspect-error-cause.js deleted file mode 100644 index ed9d8230fe0c40..00000000000000 --- a/test/message/util-inspect-error-cause.js +++ /dev/null @@ -1,54 +0,0 @@ -'use strict'; - -require('../common'); - -const { inspect } = require('util'); - -class FoobarError extends Error { - status = 'Feeling good'; -} - -const cause1 = new TypeError('Inner error'); -const cause2 = new FoobarError('Individual message', { cause: cause1 }); -cause2.extraProperties = 'Yes!'; -const cause3 = new Error('Stack causes', { cause: cause2 }); - -const cause4 = new Error('Number error cause', { cause: 42 }); -const cause5 = new Error('Object cause', { - cause: { - message: 'Unique', - name: 'Error', - stack: 'Error: Unique\n' + - ' at Module._compile (node:internal/modules/cjs/loader:827:30)', - }, -}); -const cause6 = new Error('undefined cause', { - cause: undefined, -}); - -console.log(cause4); -console.log(cause5); -console.log(cause6); - -process.nextTick(() => { - const error = new RangeError('New Stack Frames', { cause: cause2 }); - const error2 = new RangeError('New Stack Frames', { cause: cause3 }); - - inspect.defaultOptions.colors = true; - - console.log(inspect(error)); - console.log(inspect(cause3)); - console.log(inspect(error2)); - - inspect.defaultOptions.colors = false; - - console.log(inspect(error)); - console.log(inspect(cause3)); - console.log(inspect(error2)); -}); - -{ - const error = new Error('cause that throws'); - Reflect.defineProperty(error, 'cause', { get() { throw new Error(); } }); - console.log(inspect(error)); -} diff --git a/test/message/util-inspect-error-cause.out b/test/message/util-inspect-error-cause.out deleted file mode 100644 index 344ace1bc94074..00000000000000 --- a/test/message/util-inspect-error-cause.out +++ /dev/null @@ -1,191 +0,0 @@ -Error: Number error cause - at * - at * - at * - at * - at * - at * - at * - at * { - [cause]: 42 -} -Error: Object cause - at * - at * - at * - at * - at * - at * - at * - at * { - [cause]: { - message: 'Unique', - name: 'Error', - stack: 'Error: Unique\n' + - ' at Module._compile (node:internal/modules/cjs/loader:827:30)' - } -} -Error: undefined cause - at * - at * - at * - at * - at * - at * - at * - at * { - [cause]: undefined -} -Error: cause that throws - at * - at * - at * - at * - at * - at * - at * - at * { - [cause]: [Getter] -} -RangeError: New Stack Frames - at * -*[90m at *[39m { - [cause]: FoobarError: Individual message - at * - *[90m at *[39m - *[90m ... 5 lines matching cause stack trace ...*[39m - *[90m at *[39m { - status: *[32m'Feeling good'*[39m, - extraProperties: *[32m'Yes!'*[39m, - [cause]: TypeError: Inner error - at * - *[90m at *[39m - *[90m at *[39m - *[90m at *[39m - *[90m at *[39m - *[90m at *[39m - *[90m at *[39m - *[90m at *[39m - } -} -Error: Stack causes - at * -*[90m at *[39m -*[90m ... 5 lines matching cause stack trace ...*[39m -*[90m at *[39m { - [cause]: FoobarError: Individual message - at * - *[90m at *[39m - *[90m ... 5 lines matching cause stack trace ...*[39m - *[90m at *[39m { - status: *[32m'Feeling good'*[39m, - extraProperties: *[32m'Yes!'*[39m, - [cause]: TypeError: Inner error - at * - *[90m at *[39m - *[90m at *[39m - *[90m at *[39m - *[90m at *[39m - *[90m at *[39m - *[90m at *[39m - *[90m at *[39m - } -} -RangeError: New Stack Frames - at * -*[90m at *[39m { - [cause]: Error: Stack causes - at * - *[90m at *[39m - *[90m ... 5 lines matching cause stack trace ...*[39m - *[90m at *[39m { - [cause]: FoobarError: Individual message - at * - *[90m at *[39m - *[90m ... 5 lines matching cause stack trace ...*[39m - *[90m at *[39m { - status: *[32m'Feeling good'*[39m, - extraProperties: *[32m'Yes!'*[39m, - [cause]: TypeError: Inner error - at * - *[90m at *[39m - *[90m at *[39m - *[90m at *[39m - *[90m at *[39m - *[90m at *[39m - *[90m at *[39m - *[90m at *[39m - } - } -} -RangeError: New Stack Frames - at * - at * { - [cause]: FoobarError: Individual message - at * - at * - ... 5 lines matching cause stack trace ... - at * { - status: 'Feeling good', - extraProperties: 'Yes!', - [cause]: TypeError: Inner error - at * - at * - at * - at * - at * - at * - at * - at * - } -} -Error: Stack causes - at * - at * - ... 5 lines matching cause stack trace ... - at * { - [cause]: FoobarError: Individual message - at * - at * - ... 5 lines matching cause stack trace ... - at * - status: 'Feeling good', - extraProperties: 'Yes!', - [cause]: TypeError: Inner error - at * - at * - at * - at * - at * - at * - at * - at * - } -} -RangeError: New Stack Frames - at * - at * { - [cause]: Error: Stack causes - at * - at * - ... 5 lines matching cause stack trace ... - at * { - [cause]: FoobarError: Individual message - at * - at * - ... 5 lines matching cause stack trace ... - at * { - status: 'Feeling good', - extraProperties: 'Yes!', - [cause]: TypeError: Inner error - at * - at * - at * - at * - at * - at * - at * - at * - } - } -} diff --git a/test/parallel/test-node-output-util.mjs b/test/parallel/test-node-output-util.mjs new file mode 100644 index 00000000000000..2318f1087aa313 --- /dev/null +++ b/test/parallel/test-node-output-util.mjs @@ -0,0 +1,35 @@ +import * as common from '../common/index.mjs'; +import * as fixtures from '../common/fixtures.mjs'; +import * as snapshot from '../common/assertSnapshot.js'; +import { describe, it } from 'node:test'; +import { basename } from 'node:path'; +import { pathToFileURL } from 'node:url'; + +describe('util output', { concurrency: !process.env.TEST_PARALLEL }, () => { + function normalize(str) { + const baseName = basename(process.argv0 || 'node', '.exe'); + return str.replaceAll(snapshot.replaceWindowsPaths(process.cwd()), '') + .replaceAll(pathToFileURL(process.cwd()).pathname, '') + .replaceAll('//', '*') + .replaceAll(/\/(\w)/g, '*$1') + .replaceAll('*test*', '*') + .replaceAll('*fixtures*util*', '*') + .replaceAll('file:**', 'file:*/') + .replaceAll(`${baseName} --`, '* --'); + } + + const commonTransform = snapshot + .transform(snapshot.replaceWindowsLineEndings, snapshot.replaceWindowsPaths); + const defaultTransform = snapshot.transform(commonTransform, normalize, snapshot.replaceNodeVersion); + + const tests = [ + { name: 'util/util_inspect_error.js', transform: defaultTransform }, + { name: 'util/util-inspect-error-cause.js', transform: defaultTransform }, + ]; + + for (const { name, transform } of tests) { + it(name, async () => { + await snapshot.spawnAndAssert(fixtures.path(name), transform); + }); + } +});