Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 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
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(),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we cannot use this as expect is meant to work in the browser (and rootDir might not even be cwd).

If we are to do this, we'll need to pass the config object somehow

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I just carry this from line 462.

Why do not it work here?

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