From 76d1b8c76fc085bb3ac612a1a14f4a85aef3c64d Mon Sep 17 00:00:00 2001 From: Julian Dax Date: Wed, 22 Feb 2023 22:49:25 +0100 Subject: [PATCH 1/7] feat(jest-message-util): add support for AggregateErrors See https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/AggregateError --- .../__snapshots__/messages.test.ts.snap | 19 +++++++++++++++ .../src/__tests__/messages.test.ts | 22 ++++++++++++++++++ packages/jest-message-util/src/index.ts | 23 ++++++++++++++++++- 3 files changed, 63 insertions(+), 1 deletion(-) diff --git a/packages/jest-message-util/src/__tests__/__snapshots__/messages.test.ts.snap b/packages/jest-message-util/src/__tests__/__snapshots__/messages.test.ts.snap index 9f629042d425..25d41a324ffe 100644 --- a/packages/jest-message-util/src/__tests__/__snapshots__/messages.test.ts.snap +++ b/packages/jest-message-util/src/__tests__/__snapshots__/messages.test.ts.snap @@ -127,3 +127,22 @@ exports[`should return the error cause if there is one 1`] = ` " `; + +exports[`should return the inner errors of an AggregateError 1`] = ` +" ● Test suite failed to run + + AggregateError: + + at Object. (packages/jest-message-util/src/__tests__/messages.test.ts:439:22) + + Errors contained in AggregateError: + Err 1 + + at Object. (packages/jest-message-util/src/__tests__/messages.test.ts:440:7) + + Err 2 + + at Object. (packages/jest-message-util/src/__tests__/messages.test.ts:441:7) + +" +`; diff --git a/packages/jest-message-util/src/__tests__/messages.test.ts b/packages/jest-message-util/src/__tests__/messages.test.ts index 2956f0b8e62c..21d33c9d0a83 100644 --- a/packages/jest-message-util/src/__tests__/messages.test.ts +++ b/packages/jest-message-util/src/__tests__/messages.test.ts @@ -431,3 +431,25 @@ it('should return the error cause if there is one', () => { ); expect(message).toMatchSnapshot(); }); + +it('should return the inner errors of an AggregateError', () => { + // TODO remove this if when the lowest supported Node version is 15.0.0 + // See https://github.com/nodejs/node/blob/main/doc/changelogs/CHANGELOG_V15.md#v8-86---35415 + if (AggregateError) { + const aggError = new AggregateError([ + new Error('Err 1'), + new Error('Err 2'), + ]); + const message = formatExecError( + aggError, + { + rootDir: '', + testMatch: [], + }, + { + noStackTrace: false, + }, + ); + expect(message).toMatchSnapshot(); + } +}); diff --git a/packages/jest-message-util/src/index.ts b/packages/jest-message-util/src/index.ts index bf03d9d547d4..6636bc525a0a 100644 --- a/packages/jest-message-util/src/index.ts +++ b/packages/jest-message-util/src/index.ts @@ -137,6 +137,7 @@ export const formatExecError = ( let message, stack; let cause = ''; + const subErrors = []; if (typeof error === 'string' || !error) { error || (error = 'EMPTY ERROR'); @@ -172,6 +173,20 @@ export const formatExecError = ( cause += `${prefix}${formatted}`; } } + if ('errors' in error && Array.isArray(error.errors)) { + for (const subError of error.errors) { + subErrors.push( + formatExecError( + subError, + config, + options, + testPath, + reuseMessage, + true, + ), + ); + } + } } if (cause !== '') { cause = indentAllLines(cause); @@ -210,8 +225,14 @@ export const formatExecError = ( messageToUse = `${EXEC_ERROR_MESSAGE}\n\n${message}`; } const title = noTitle ? '' : `${TITLE_INDENT + TITLE_BULLET}`; + const subErrorStr = + subErrors.length > 0 + ? indentAllLines( + `\n\nErrors contained in AggregateError:\n${subErrors.join('\n')}`, + ) + : ''; - return `${title + messageToUse + stack + cause}\n`; + return `${title + messageToUse + stack + cause + subErrorStr}\n`; }; const removeInternalStackEntries = ( From 1e04ff77d12f92407880b2a422608d508cec1d82 Mon Sep 17 00:00:00 2001 From: Julian Dax Date: Wed, 22 Feb 2023 23:04:12 +0100 Subject: [PATCH 2/7] chore: add entry in CHANGELOG.md --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index cecb1ed5096b..4c5067418e92 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,7 @@ - `[jest-cli, jest-config, @jest/core, jest-haste-map, @jest/reporters, jest-runner, jest-runtime, @jest/types]` Add `workerThreads` configuration option to allow using [worker threads](https://nodejs.org/dist/latest/docs/api/worker_threads.html) for parallelization ([#13939](https://github.com/facebook/jest/pull/13939)) - `[jest-worker]` Add `start` method to worker farms ([#13937](https://github.com/facebook/jest/pull/13937)) +- `[jest-message-util]` Add support for [AggregateErrors](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/AggregateError) ([#13946](https://github.com/facebook/jest/pull/13946)) ### Fixes From a0e32dc3d201b8f5d3aad99f97de7f1462ea946f Mon Sep 17 00:00:00 2001 From: Julian Dax Date: Thu, 23 Feb 2023 00:14:44 +0100 Subject: [PATCH 3/7] fix: fix unit tests on Node 14 at least I hope it does :D --- packages/jest-message-util/src/__tests__/messages.test.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/jest-message-util/src/__tests__/messages.test.ts b/packages/jest-message-util/src/__tests__/messages.test.ts index 21d33c9d0a83..55087aeeba80 100644 --- a/packages/jest-message-util/src/__tests__/messages.test.ts +++ b/packages/jest-message-util/src/__tests__/messages.test.ts @@ -435,7 +435,7 @@ it('should return the error cause if there is one', () => { it('should return the inner errors of an AggregateError', () => { // TODO remove this if when the lowest supported Node version is 15.0.0 // See https://github.com/nodejs/node/blob/main/doc/changelogs/CHANGELOG_V15.md#v8-86---35415 - if (AggregateError) { + if (typeof AggregateError !== 'undefined') { const aggError = new AggregateError([ new Error('Err 1'), new Error('Err 2'), From 392e9180dd75913b7ee524f5e922f82cd2672f38 Mon Sep 17 00:00:00 2001 From: Simen Bekkhus Date: Thu, 23 Feb 2023 08:41:29 +0100 Subject: [PATCH 4/7] Revert "fix: fix unit tests on Node 14" This reverts commit a0e32dc3d201b8f5d3aad99f97de7f1462ea946f. --- packages/jest-message-util/src/__tests__/messages.test.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/jest-message-util/src/__tests__/messages.test.ts b/packages/jest-message-util/src/__tests__/messages.test.ts index 55087aeeba80..21d33c9d0a83 100644 --- a/packages/jest-message-util/src/__tests__/messages.test.ts +++ b/packages/jest-message-util/src/__tests__/messages.test.ts @@ -435,7 +435,7 @@ it('should return the error cause if there is one', () => { it('should return the inner errors of an AggregateError', () => { // TODO remove this if when the lowest supported Node version is 15.0.0 // See https://github.com/nodejs/node/blob/main/doc/changelogs/CHANGELOG_V15.md#v8-86---35415 - if (typeof AggregateError !== 'undefined') { + if (AggregateError) { const aggError = new AggregateError([ new Error('Err 1'), new Error('Err 2'), From 387fa5b79f5c228a1e5e5dc66ec3dce6524d5e05 Mon Sep 17 00:00:00 2001 From: Simen Bekkhus Date: Thu, 23 Feb 2023 08:44:41 +0100 Subject: [PATCH 5/7] skip test on node 14 --- .../__snapshots__/messages.test.ts.snap | 42 +++++++++--------- .../src/__tests__/messages.test.ts | 43 ++++++++++--------- 2 files changed, 44 insertions(+), 41 deletions(-) diff --git a/packages/jest-message-util/src/__tests__/__snapshots__/messages.test.ts.snap b/packages/jest-message-util/src/__tests__/__snapshots__/messages.test.ts.snap index 25d41a324ffe..57c2a440f981 100644 --- a/packages/jest-message-util/src/__tests__/__snapshots__/messages.test.ts.snap +++ b/packages/jest-message-util/src/__tests__/__snapshots__/messages.test.ts.snap @@ -71,6 +71,25 @@ exports[`no stack 1`] = ` " `; +exports[`on node >=15.0.0 should return the inner errors of an AggregateError 1`] = ` +" ● Test suite failed to run + + AggregateError: + + at Object. (packages/jest-message-util/src/__tests__/messages.test.ts:441:24) + + Errors contained in AggregateError: + Err 1 + + at Object. (packages/jest-message-util/src/__tests__/messages.test.ts:442:9) + + Err 2 + + at Object. (packages/jest-message-util/src/__tests__/messages.test.ts:443:9) + +" +`; + exports[`retains message in babel code frame error 1`] = ` " Babel test @@ -118,31 +137,12 @@ exports[`should return the error cause if there is one 1`] = ` Test exception - at Object. (packages/jest-message-util/src/__tests__/messages.test.ts:418:17) + at Object. (packages/jest-message-util/src/__tests__/messages.test.ts:419:17) Cause: Cause Error - at Object. (packages/jest-message-util/src/__tests__/messages.test.ts:421:17) - -" -`; - -exports[`should return the inner errors of an AggregateError 1`] = ` -" ● Test suite failed to run - - AggregateError: - - at Object. (packages/jest-message-util/src/__tests__/messages.test.ts:439:22) - - Errors contained in AggregateError: - Err 1 - - at Object. (packages/jest-message-util/src/__tests__/messages.test.ts:440:7) - - Err 2 - - at Object. (packages/jest-message-util/src/__tests__/messages.test.ts:441:7) + at Object. (packages/jest-message-util/src/__tests__/messages.test.ts:422:17) " `; diff --git a/packages/jest-message-util/src/__tests__/messages.test.ts b/packages/jest-message-util/src/__tests__/messages.test.ts index 21d33c9d0a83..e9bebb3f6e3a 100644 --- a/packages/jest-message-util/src/__tests__/messages.test.ts +++ b/packages/jest-message-util/src/__tests__/messages.test.ts @@ -9,6 +9,7 @@ import {readFileSync} from 'graceful-fs'; import slash = require('slash'); import tempy = require('tempy'); +import {onNodeVersions} from '@jest/test-utils'; import { formatExecError, formatResultsErrors, @@ -432,24 +433,26 @@ it('should return the error cause if there is one', () => { expect(message).toMatchSnapshot(); }); -it('should return the inner errors of an AggregateError', () => { - // TODO remove this if when the lowest supported Node version is 15.0.0 - // See https://github.com/nodejs/node/blob/main/doc/changelogs/CHANGELOG_V15.md#v8-86---35415 - if (AggregateError) { - const aggError = new AggregateError([ - new Error('Err 1'), - new Error('Err 2'), - ]); - const message = formatExecError( - aggError, - { - rootDir: '', - testMatch: [], - }, - { - noStackTrace: false, - }, - ); - expect(message).toMatchSnapshot(); - } +// TODO remove this wrapper when the lowest supported Node version is v16 +onNodeVersions('>=15.0.0', () => { + it('should return the inner errors of an AggregateError', () => { + // See https://github.com/nodejs/node/blob/main/doc/changelogs/CHANGELOG_V15.md#v8-86---35415 + if (AggregateError) { + const aggError = new AggregateError([ + new Error('Err 1'), + new Error('Err 2'), + ]); + const message = formatExecError( + aggError, + { + rootDir: '', + testMatch: [], + }, + { + noStackTrace: false, + }, + ); + expect(message).toMatchSnapshot(); + } + }); }); From 5aff93af1945175298310d44f6b493aa77883892 Mon Sep 17 00:00:00 2001 From: Simen Bekkhus Date: Thu, 23 Feb 2023 08:45:13 +0100 Subject: [PATCH 6/7] move changelog entry --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 6e87a6a3ef14..9216ef088d6a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,8 +4,8 @@ - `[jest-changed-files]` Support Sapling ([#13941](https://github.com/facebook/jest/pull/13941)) - `[jest-cli, jest-config, @jest/core, jest-haste-map, @jest/reporters, jest-runner, jest-runtime, @jest/types]` Add `workerThreads` configuration option to allow using [worker threads](https://nodejs.org/dist/latest/docs/api/worker_threads.html) for parallelization ([#13939](https://github.com/facebook/jest/pull/13939)) -- `[jest-worker]` Add `start` method to worker farms ([#13937](https://github.com/facebook/jest/pull/13937)) - `[jest-message-util]` Add support for [AggregateErrors](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/AggregateError) ([#13946](https://github.com/facebook/jest/pull/13946)) +- `[jest-worker]` Add `start` method to worker farms ([#13937](https://github.com/facebook/jest/pull/13937)) ### Fixes From fffc12d388f2a76fff6fb7d6d89cdb71713a66e6 Mon Sep 17 00:00:00 2001 From: Simen Bekkhus Date: Thu, 23 Feb 2023 09:39:41 +0100 Subject: [PATCH 7/7] Update CHANGELOG.md --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 9216ef088d6a..b691629f5139 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,7 +4,7 @@ - `[jest-changed-files]` Support Sapling ([#13941](https://github.com/facebook/jest/pull/13941)) - `[jest-cli, jest-config, @jest/core, jest-haste-map, @jest/reporters, jest-runner, jest-runtime, @jest/types]` Add `workerThreads` configuration option to allow using [worker threads](https://nodejs.org/dist/latest/docs/api/worker_threads.html) for parallelization ([#13939](https://github.com/facebook/jest/pull/13939)) -- `[jest-message-util]` Add support for [AggregateErrors](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/AggregateError) ([#13946](https://github.com/facebook/jest/pull/13946)) +- `[jest-message-util]` Add support for [AggregateError](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/AggregateError) ([#13946](https://github.com/facebook/jest/pull/13946)) - `[jest-worker]` Add `start` method to worker farms ([#13937](https://github.com/facebook/jest/pull/13937)) ### Fixes