diff --git a/CHANGELOG.md b/CHANGELOG.md index d5912db61c13..b691629f5139 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +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 [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 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..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,12 +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) + 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 2956f0b8e62c..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, @@ -431,3 +432,27 @@ it('should return the error cause if there is one', () => { ); 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(); + } + }); +}); 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 = (