Skip to content

Commit 1d704bd

Browse files
fix: print AggregateErrors to display
Closes jestjs#14704
1 parent 0a0a9f7 commit 1d704bd

File tree

4 files changed

+57
-13
lines changed

4 files changed

+57
-13
lines changed

package.json

+1
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,7 @@
101101
"jest-jasmine-ci": "yarn jest-jasmine --color --config jest.config.ci.mjs",
102102
"jest-coverage": "yarn jest --coverage",
103103
"lint": "eslint . --cache --ext js,jsx,cjs,mjs,ts,tsx,md",
104+
"lint:fix": "eslint ./packages/expect/src/toThrowMatchers.ts --cache --fix",
104105
"lint:prettier-script": "prettier . \"!**/*.{js,jsx,cjs,mjs,ts,tsx}\" --cache",
105106
"lint:prettier": "yarn lint:prettier-script --write",
106107
"lint:prettier:ci": "yarn lint:prettier-script --check",

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

+30
Original file line numberDiff line numberDiff line change
@@ -332,6 +332,36 @@ describe('toThrow', () => {
332332
});
333333
});
334334

335+
describe('aggregate-errors', () => {
336+
const fetchFromApi1 = Promise.reject(new Error('API 1 failed'));
337+
const fetchFromApi2 = Promise.reject(new Error('API 2 failed'));
338+
const promiseAny = Promise.any([fetchFromApi1, fetchFromApi2]);
339+
340+
test('string', () => {
341+
jestExpect(promiseAny).rejects.toThrow('All promises were rejected');
342+
});
343+
344+
test('undefined', () => {
345+
jestExpect(promiseAny).rejects.toThrow();
346+
});
347+
348+
test('asymmetricMatch', () => {
349+
jestExpect(promiseAny).rejects.toThrow(
350+
expect.objectContaining({
351+
message: 'All promises were rejected',
352+
}),
353+
);
354+
});
355+
356+
test('regexp', () => {
357+
jestExpect(promiseAny).rejects.toThrow(/All promises were rejected/);
358+
});
359+
360+
test('class', () => {
361+
jestExpect(promiseAny).rejects.toThrow(AggregateError);
362+
});
363+
});
364+
335365
describe('asymmetric', () => {
336366
describe('any-Class', () => {
337367
describe('pass', () => {

packages/expect/src/__tests__/tsconfig.json

+1-1
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

+25-12
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,11 @@ import {
2020
printReceived,
2121
printWithType,
2222
} from 'jest-matcher-utils';
23-
import {formatStackTrace, separateMessageFromStack} from 'jest-message-util';
23+
import {
24+
formatExecError,
25+
formatStackTrace,
26+
separateMessageFromStack,
27+
} from 'jest-message-util';
2428
import {
2529
printExpectedConstructorName,
2630
printExpectedConstructorNameNot,
@@ -453,19 +457,28 @@ const formatReceived = (
453457
return '';
454458
};
455459

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

470483
function createMessageAndCauseMessage(error: Error): string {
471484
if (error.cause instanceof Error) {

0 commit comments

Comments
 (0)