Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: print AggregateError to display #15346

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading