Skip to content

Commit 73c5fe9

Browse files
BondarenkoAlexCasperjuelcpojer
authored
fix: print AggregateError to display (#15346)
Co-authored-by: Casper Juel <[email protected]> Co-authored-by: Christoph Nakazawa <[email protected]>
1 parent 54673fd commit 73c5fe9

File tree

4 files changed

+57
-13
lines changed

4 files changed

+57
-13
lines changed

CHANGELOG.md

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

5555
### Fixes
5656

57+
- `[expect]` Show `AggregateError` to display ([#15346](https://github.com/facebook/jest/pull/15346))
5758
- `[*]` Replace `exit` with `exit-x` ([#15399](https://github.com/jestjs/jest/pull/15399))
5859
- `[babel-plugin-jest-hoist]` Use `denylist` instead of the deprecated `blacklist` for Babel 8 support ([#14109](https://github.com/jestjs/jest/pull/14109))
5960
- `[babel-plugin-jest-hoist]` Do not rely on buggy Babel behaviour ([#15415](https://github.com/jestjs/jest/pull/15415))

packages/expect/src/__tests__/toThrowMatchers.test.ts

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -363,6 +363,36 @@ describe('toThrow', () => {
363363
});
364364
});
365365

366+
describe('aggregate-errors', () => {
367+
const fetchFromApi1 = Promise.reject(new Error('API 1 failed'));
368+
const fetchFromApi2 = Promise.reject(new Error('API 2 failed'));
369+
const promiseAny = Promise.any([fetchFromApi1, fetchFromApi2]);
370+
371+
test('string', () => {
372+
jestExpect(promiseAny).rejects.toThrow('All promises were rejected');
373+
});
374+
375+
test('undefined', () => {
376+
jestExpect(promiseAny).rejects.toThrow();
377+
});
378+
379+
test('asymmetricMatch', () => {
380+
jestExpect(promiseAny).rejects.toThrow(
381+
expect.objectContaining({
382+
message: 'All promises were rejected',
383+
}),
384+
);
385+
});
386+
387+
test('regexp', () => {
388+
jestExpect(promiseAny).rejects.toThrow(/All promises were rejected/);
389+
});
390+
391+
test('class', () => {
392+
jestExpect(promiseAny).rejects.toThrow(AggregateError);
393+
});
394+
});
395+
366396
describe('asymmetric', () => {
367397
describe('any-Class', () => {
368398
describe('pass', () => {

packages/expect/src/__tests__/tsconfig.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"extends": "../../../../tsconfig.test.json",
33
"compilerOptions": {
4-
"lib": ["es2022.error"],
4+
"lib": ["es2022.error", "es2021.promise"],
55
"types": ["node", "@jest/test-globals"]
66
},
77
"include": ["./**/*"],

packages/expect/src/toThrowMatchers.ts

Lines changed: 25 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,11 @@ import {
1818
printReceived,
1919
printWithType,
2020
} from 'jest-matcher-utils';
21-
import {formatStackTrace, separateMessageFromStack} from 'jest-message-util';
21+
import {
22+
formatExecError,
23+
formatStackTrace,
24+
separateMessageFromStack,
25+
} from 'jest-message-util';
2226
import {
2327
printExpectedConstructorName,
2428
printExpectedConstructorNameNot,
@@ -451,19 +455,28 @@ const formatReceived = (
451455
return '';
452456
};
453457

454-
const formatStack = (thrown: Thrown | null) =>
455-
thrown === null || !thrown.isError
456-
? ''
457-
: formatStackTrace(
458+
const formatStack = (thrown: Thrown | null) => {
459+
if (thrown === null || !thrown.isError) {
460+
return '';
461+
} else {
462+
const config = {
463+
rootDir: process.cwd(),
464+
testMatch: [],
465+
};
466+
const options = {
467+
noStackTrace: false,
468+
};
469+
if (thrown.value instanceof AggregateError) {
470+
return formatExecError(thrown.value, config, options);
471+
} else {
472+
return formatStackTrace(
458473
separateMessageFromStack(thrown.value.stack!).stack,
459-
{
460-
rootDir: process.cwd(),
461-
testMatch: [],
462-
},
463-
{
464-
noStackTrace: false,
465-
},
474+
config,
475+
options,
466476
);
477+
}
478+
}
479+
};
467480

468481
function createMessageAndCause(error: Error) {
469482
if (error.cause) {

0 commit comments

Comments
 (0)