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(expect): improve the error message when nothing is thrown when ing toThrow #3979

Merged
merged 5 commits into from
Sep 18, 2023
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
10 changes: 10 additions & 0 deletions packages/expect/src/jest-expect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -519,12 +519,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') {
Expand Down
16 changes: 16 additions & 0 deletions test/core/test/jest-expect.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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()', () => {
Expand Down
Loading