Skip to content

Commit

Permalink
fix: show AggregateError to display
Browse files Browse the repository at this point in the history
  • Loading branch information
BondarenkoAlex committed Oct 17, 2024
1 parent 0a0a9f7 commit d9d869a
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 13 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@

### Fixes

- `[expect]` Show `AggregateError` to display ([#15346](https://github.com/facebook/jest/pull/15346))
- `[babel-plugin-jest-hoist]` Use `denylist` instead of the deprecated `blacklist` for Babel 8 support ([#14109](https://github.com/jestjs/jest/pull/14109))
- `[expect]` Check error instance type for `toThrow/toThrowError` ([#14576](https://github.com/jestjs/jest/pull/14576))
- `[expect]` Improve diff for failing `expect.objectContaining` ([#15038](https://github.com/jestjs/jest/pull/15038))
Expand Down
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 d9d869a

Please sign in to comment.