Skip to content

Commit a1ceb9a

Browse files
authored
feat(jest-message-util): add support for AggregateError (#13946)
1 parent 4e02aef commit a1ceb9a

File tree

4 files changed

+69
-3
lines changed

4 files changed

+69
-3
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
- `[jest-changed-files]` Support Sapling ([#13941](https://github.com/facebook/jest/pull/13941))
66
- `[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))
7+
- `[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))
78
- `[jest-worker]` Add `start` method to worker farms ([#13937](https://github.com/facebook/jest/pull/13937))
89

910
### Fixes

packages/jest-message-util/src/__tests__/__snapshots__/messages.test.ts.snap

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,25 @@ exports[`no stack 1`] = `
7171
"
7272
`;
7373
74+
exports[`on node >=15.0.0 should return the inner errors of an AggregateError 1`] = `
75+
" <bold>● </intensity>Test suite failed to run
76+
77+
AggregateError:
78+
79+
<dim>at Object.<anonymous> (</intensity>packages/jest-message-util/src/__tests__/messages.test.ts<dim>:441:24)</intensity>
80+
81+
Errors contained in AggregateError:
82+
Err 1
83+
84+
<dim>at Object.<anonymous> (</intensity>packages/jest-message-util/src/__tests__/messages.test.ts<dim>:442:9)</intensity>
85+
86+
Err 2
87+
88+
<dim>at Object.<anonymous> (</intensity>packages/jest-message-util/src/__tests__/messages.test.ts<dim>:443:9)</intensity>
89+
90+
"
91+
`;
92+
7493
exports[`retains message in babel code frame error 1`] = `
7594
"<bold><red> <bold>● </intensity><bold>Babel test</color></intensity>
7695
@@ -118,12 +137,12 @@ exports[`should return the error cause if there is one 1`] = `
118137
119138
Test exception
120139
121-
<dim>at Object.<anonymous> (</intensity>packages/jest-message-util/src/__tests__/messages.test.ts<dim>:418:17)</intensity>
140+
<dim>at Object.<anonymous> (</intensity>packages/jest-message-util/src/__tests__/messages.test.ts<dim>:419:17)</intensity>
122141
123142
Cause:
124143
Cause Error
125144
126-
<dim>at Object.<anonymous> (</intensity>packages/jest-message-util/src/__tests__/messages.test.ts<dim>:421:17)</intensity>
145+
<dim>at Object.<anonymous> (</intensity>packages/jest-message-util/src/__tests__/messages.test.ts<dim>:422:17)</intensity>
127146
128147
"
129148
`;

packages/jest-message-util/src/__tests__/messages.test.ts

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import {readFileSync} from 'graceful-fs';
1010
import slash = require('slash');
1111
import tempy = require('tempy');
12+
import {onNodeVersions} from '@jest/test-utils';
1213
import {
1314
formatExecError,
1415
formatResultsErrors,
@@ -431,3 +432,27 @@ it('should return the error cause if there is one', () => {
431432
);
432433
expect(message).toMatchSnapshot();
433434
});
435+
436+
// TODO remove this wrapper when the lowest supported Node version is v16
437+
onNodeVersions('>=15.0.0', () => {
438+
it('should return the inner errors of an AggregateError', () => {
439+
// See https://github.com/nodejs/node/blob/main/doc/changelogs/CHANGELOG_V15.md#v8-86---35415
440+
if (AggregateError) {
441+
const aggError = new AggregateError([
442+
new Error('Err 1'),
443+
new Error('Err 2'),
444+
]);
445+
const message = formatExecError(
446+
aggError,
447+
{
448+
rootDir: '',
449+
testMatch: [],
450+
},
451+
{
452+
noStackTrace: false,
453+
},
454+
);
455+
expect(message).toMatchSnapshot();
456+
}
457+
});
458+
});

packages/jest-message-util/src/index.ts

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,7 @@ export const formatExecError = (
137137

138138
let message, stack;
139139
let cause = '';
140+
const subErrors = [];
140141

141142
if (typeof error === 'string' || !error) {
142143
error || (error = 'EMPTY ERROR');
@@ -172,6 +173,20 @@ export const formatExecError = (
172173
cause += `${prefix}${formatted}`;
173174
}
174175
}
176+
if ('errors' in error && Array.isArray(error.errors)) {
177+
for (const subError of error.errors) {
178+
subErrors.push(
179+
formatExecError(
180+
subError,
181+
config,
182+
options,
183+
testPath,
184+
reuseMessage,
185+
true,
186+
),
187+
);
188+
}
189+
}
175190
}
176191
if (cause !== '') {
177192
cause = indentAllLines(cause);
@@ -210,8 +225,14 @@ export const formatExecError = (
210225
messageToUse = `${EXEC_ERROR_MESSAGE}\n\n${message}`;
211226
}
212227
const title = noTitle ? '' : `${TITLE_INDENT + TITLE_BULLET}`;
228+
const subErrorStr =
229+
subErrors.length > 0
230+
? indentAllLines(
231+
`\n\nErrors contained in AggregateError:\n${subErrors.join('\n')}`,
232+
)
233+
: '';
213234

214-
return `${title + messageToUse + stack + cause}\n`;
235+
return `${title + messageToUse + stack + cause + subErrorStr}\n`;
215236
};
216237

217238
const removeInternalStackEntries = (

0 commit comments

Comments
 (0)