Skip to content

Commit

Permalink
fix: print AggregateErrors to display
Browse files Browse the repository at this point in the history
Closes #14704
  • Loading branch information
BondarenkoAlex committed Oct 17, 2024
1 parent 0a0a9f7 commit 1d704bd
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 13 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@
"jest-jasmine-ci": "yarn jest-jasmine --color --config jest.config.ci.mjs",
"jest-coverage": "yarn jest --coverage",
"lint": "eslint . --cache --ext js,jsx,cjs,mjs,ts,tsx,md",
"lint:fix": "eslint ./packages/expect/src/toThrowMatchers.ts --cache --fix",
"lint:prettier-script": "prettier . \"!**/*.{js,jsx,cjs,mjs,ts,tsx}\" --cache",
"lint:prettier": "yarn lint:prettier-script --write",
"lint:prettier:ci": "yarn lint:prettier-script --check",
Expand Down
30 changes: 30 additions & 0 deletions packages/expect/src/__tests__/toThrowMatchers.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -332,6 +332,36 @@ describe('toThrow', () => {
});
});

describe('aggregate-errors', () => {
const fetchFromApi1 = Promise.reject(new Error('API 1 failed'));
const fetchFromApi2 = Promise.reject(new Error('API 2 failed'));
const promiseAny = Promise.any([fetchFromApi1, fetchFromApi2]);

test('string', () => {
jestExpect(promiseAny).rejects.toThrow('All promises were rejected');
});

test('undefined', () => {
jestExpect(promiseAny).rejects.toThrow();
});

test('asymmetricMatch', () => {
jestExpect(promiseAny).rejects.toThrow(
expect.objectContaining({
message: 'All promises were rejected',
}),
);
});

test('regexp', () => {
jestExpect(promiseAny).rejects.toThrow(/All promises were rejected/);
});

test('class', () => {
jestExpect(promiseAny).rejects.toThrow(AggregateError);
});
});

describe('asymmetric', () => {
describe('any-Class', () => {
describe('pass', () => {
Expand Down
2 changes: 1 addition & 1 deletion packages/expect/src/__tests__/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"extends": "../../../../tsconfig.test.json",
"compilerOptions": {
"lib": ["es2022.error"],
"lib": ["es2022.error", "es2021.promise"],
"types": ["node", "@jest/test-globals"]
},
"include": ["./**/*"],
Expand Down
37 changes: 25 additions & 12 deletions packages/expect/src/toThrowMatchers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,11 @@ import {
printReceived,
printWithType,
} from 'jest-matcher-utils';
import {formatStackTrace, separateMessageFromStack} from 'jest-message-util';
import {
formatExecError,
formatStackTrace,
separateMessageFromStack,
} from 'jest-message-util';
import {
printExpectedConstructorName,
printExpectedConstructorNameNot,
Expand Down Expand Up @@ -453,19 +457,28 @@ const formatReceived = (
return '';
};

const formatStack = (thrown: Thrown | null) =>
thrown === null || !thrown.isError
? ''
: formatStackTrace(
const formatStack = (thrown: Thrown | null) => {
if (thrown === null || !thrown.isError) {
return '';
} else {
const config = {
rootDir: process.cwd(),
testMatch: [],
};
const options = {
noStackTrace: false,
};
if (thrown.value instanceof AggregateError) {
return formatExecError(thrown.value, config, options);
} else {
return formatStackTrace(
separateMessageFromStack(thrown.value.stack!).stack,
{
rootDir: process.cwd(),
testMatch: [],
},
{
noStackTrace: false,
},
config,
options,
);
}
}
};

function createMessageAndCauseMessage(error: Error): string {
if (error.cause instanceof Error) {
Expand Down

0 comments on commit 1d704bd

Please sign in to comment.