Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
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 @@ -15,6 +15,7 @@
### Fixes

- `[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))
- `[jest-circus]` [**BREAKING**] Prevent false test failures caused by promise rejections handled asynchronously ([#14315](https://github.com/jestjs/jest/pull/14315))
- `[@jest/expect-utils]` Fix comparison of `DataView` ([#14408](https://github.com/jestjs/jest/pull/14408))
- `[jest-leak-detector]` Make leak-detector more aggressive when running GC ([#14526](https://github.com/jestjs/jest/pull/14526))
Expand Down
6 changes: 6 additions & 0 deletions packages/expect/src/__tests__/toThrowMatchers.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -169,10 +169,16 @@ describe.each(['toThrowError', 'toThrow'] as const)('%s', toThrow => {
jestExpect(() => {
throw new Err();
})[toThrow](CustomError);
jestExpect(() => {
throw new SubErr();
})[toThrow](new SubErr());
jestExpect(() => {
throw new Err();
}).not[toThrow](Err2);
jestExpect(() => {}).not[toThrow](Err);
jestExpect(() => {
throw new SubErr();
}).not[toThrow](new SubSubErr());
});

test('did not throw at all', () => {
Expand Down
9 changes: 8 additions & 1 deletion packages/expect/src/toThrowMatchers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -228,10 +228,17 @@ const toThrowExpectedObject = (
const expectedMessageAndCause = createMessageAndCause(expected);
const thrownMessageAndCause =
thrown === null ? null : createMessageAndCause(thrown.value);
const isCompareErrorInstance = thrown?.isError && expected instanceof Error;
const isExpectedCustomErrorInstance =
expected.constructor.name !== Error.name;

const pass =
thrown !== null &&
thrown.message === expected.message &&
thrownMessageAndCause === expectedMessageAndCause;
thrownMessageAndCause === expectedMessageAndCause &&
(!isCompareErrorInstance ||
!isExpectedCustomErrorInstance ||
thrown.value instanceof expected.constructor);

const message = pass
? () =>
Expand Down