-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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
toThrowError() doesn’t catch async throws #2262
Comments
Actually, I have found that this works, and there are existing tests for this: await expect((async () => {
await asyncFunctionThatThrows();
})()).rejects.toThrowError(); But this seems to be a deviation from the Jest API. If this is the intended way to run async expectations, could this be added to the docs? Or is there something here about allowing the simpler syntax for parity with Jest? I do think there is some bug here, because |
This is expected behaviour. If you look at type definition,
It also doesn't work in Jest, and not a deviation from Jest API. You can rewrite your example as: await expect(asyncFunctionThatThrows).rejects.toThrowError(); We can add a note in docs on how to test it asynchronously, and redirect to |
Sounds good to me. And thanks for double-checking. Feel free to close this issue as it seems to be documentation then (unless you want to track that via this issue). Also I am glad to open up a PR for that documentation if desired! |
PR is always welcome 👍 |
Sorry for the delay on this. I’m still planning on opening a PR to update the docs. Before I do, I wanted to double-check the current behavior of Jest regarding async Anyway, I just haven’t set time aside for that but will soon. I want to make sure my knowledge (and the docs) are accurate |
Hello. Faced the same problem and found a solution with |
Spent a little time comparing Vitest to Jest, and at least in simple examples I couldn’t find any discrepancy between how both handle |
Describe the bug
When
expect(…).toThrowError()
is used with an async function, Vitest doesn’t catch it.Vitest reports that the function didn’t throw, and there was an uncaught exception that happened elsewhere.
Note that
expect.toThrowError()
works just fine with synchronous throws.I’d love to try submitting a PR to fix this if desired 🙏
Reproduction
https://github.com/drwpow/vitest-throw-repro
System Info
Used Package Manager
npm
Validations
The text was updated successfully, but these errors were encountered: