diff --git a/packages/expect/src/jest-expect.ts b/packages/expect/src/jest-expect.ts index 24215e9cb00e..471e9bdee9cf 100644 --- a/packages/expect/src/jest-expect.ts +++ b/packages/expect/src/jest-expect.ts @@ -512,12 +512,22 @@ export const JestChaiExpect: ChaiPlugin = (chai, utils) => { } } else { + let isThrow = false try { obj() } catch (err) { + isThrow = true thrown = err } + + if (!isThrow && !isNot) { + const message = utils.flag(this, 'message') || 'expected function to throw an error, but it didn\'t' + const error = { + showDiff: false, + } + throw new AssertionError(message, error, utils.flag(this, 'ssfi')) + } } if (typeof expected === 'function') { diff --git a/test/core/test/jest-expect.test.ts b/test/core/test/jest-expect.test.ts index 792372fe20a2..3a14899a41ab 100644 --- a/test/core/test/jest-expect.test.ts +++ b/test/core/test/jest-expect.test.ts @@ -368,6 +368,22 @@ describe('jest-expect', () => { }, ]) }) + + describe('toThrow', () => { + it('error wasn\'t thrown', () => { + expect(() => { + expect(() => { + }).toThrow(Error) + }).toThrowErrorMatchingInlineSnapshot('"expected function to throw an error, but it didn\'t"') + }) + + it('async wasn\'t awaited', () => { + expect(() => { + expect(async () => { + }).toThrow(Error) + }).toThrowErrorMatchingInlineSnapshot('"expected function to throw an error, but it didn\'t"') + }) + }) }) describe('.toStrictEqual()', () => {