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 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion packages/expect/src/jest-expect.ts
Expand Up @@ -529,7 +529,7 @@ export const JestChaiExpect: ChaiPlugin = (chai, utils) => {
}

if (!isThrow && !isNot) {
const message = utils.flag(this, 'message') || 'expected function to throw an error, but it didn`t'
const message = utils.flag(this, 'message') || 'expected function to throw an error, but it didn\'t'
const error = {
showDiff: false,
}
Expand Down
19 changes: 12 additions & 7 deletions test/core/test/jest-expect.test.ts
Expand Up @@ -332,13 +332,6 @@ describe('jest-expect', () => {
expect(null).toBe(true)
})

it.fails('toThrow should failure when does not throw ', () => {
expect(() => {
}).toThrow(Error)
expect(async () => {
}).toThrow(Error)
})

// https://jestjs.io/docs/expect#tostrictequalvalue

class LaCroix {
Expand Down Expand Up @@ -375,6 +368,18 @@ describe('jest-expect', () => {
},
])
})

it('toThrow didn\'t throw', () => {
expect(() => {
expect(async () => {
}).toThrow(Error)
Copy link
Member

Choose a reason for hiding this comment

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

Async error should be with .rejects:

await expect(async () => {}).rejects.toThrow(Error)

Copy link
Member Author

Choose a reason for hiding this comment

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

The results are consistent with jest
image

Copy link
Member

Choose a reason for hiding this comment

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

Yes, but it doesn't mean that code is correct. Function didn't finish running yet

Copy link
Member

Choose a reason for hiding this comment

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

toThrow receives a Promise, it doesn't wait it to resolve to ACTUALLY check that error didn't happen

If you throw an error there, you will get unhandled promise error, not toThrow error

Copy link
Member Author

Choose a reason for hiding this comment

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

This test case actually comes from here, which is exactly what I wanted to test

Copy link
Member Author

Choose a reason for hiding this comment

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

Async error should be with .rejects:

await expect(async () => {}).rejects.toThrow(Error)

I got an error
image

Copy link
Member

Choose a reason for hiding this comment

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

This test case actually comes from #3941, which is exactly what I wanted to test

Well, then your error description is not enough "but it didn't threw" doesn't really tell much. In the current example there are two errors: 1) async wasn't awaited, 2) error wasn't thrown, both can be tested independently

I got an error

Please, read how to test asynchronous code: https://vitest.dev/api/expect.html#resolves

}).toThrowErrorMatchingInlineSnapshot('"expected function to throw an error, but it didn\'t"')

expect(() => {
expect(() => {
}).toThrow(Error)
}).toThrowErrorMatchingInlineSnapshot('"expected function to throw an error, but it didn\'t"')
})
})

describe('.toStrictEqual()', () => {
Expand Down