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

Support testing the unhandled promise rejection #5796

Open
4 tasks done
kibertoad opened this issue May 30, 2024 · 3 comments
Open
4 tasks done

Support testing the unhandled promise rejection #5796

kibertoad opened this issue May 30, 2024 · 3 comments
Labels
p3-minor-bug An edge case that only affects very specific usage (priority)

Comments

@kibertoad
Copy link

Clear and concise description of the problem

Currently vitest fails a test run if it detects an unhandled rejection having been thrown:

Vitest caught 1 unhandled error during the test run.
This might cause false positive tests. Resolve unhandled errors to make sure your tests are not affected.

However, sometimes this is a desireable behaviour, especially when we are testing global unhandled rejection handlers.

Suggested solution

Several options are possible:

  • Only report as a problem unhandled rejections that were not handled during the test execution (e. g. if there is no `process.on('unhandledRejection' handler that would consume the event);
  • Make it possible to mark tests as expecting the unhandled rejections (e. g. syntax similar to expect.assertions(1) - expect.unhandledRejections(1))

Alternative

No response

Additional context

No response

Validations

@fregante
Copy link
Contributor

fregante commented Jun 21, 2024

I'm interested in this as well. With event handlers and callbacks in general, errors don't always make it back to the original call site, so they're thrown and uncaught/unhandled.

It'd be great if Vitest had an easy way "expect" these.

Last time I needed this I had to register my own unhandled rejection listener, probably breaking Vitest's own. Example:

https://stackoverflow.com/a/58844172/288906

@sheremet-va
Copy link
Member

Last time I needed this I had to register my own unhandled rejection listener, probably breaking Vitest's own. Example:

I think this is what I would expect from the test to be honest.

I am not against having a built-in assertion for this. What API would you prefer? It would have to be async.

@kibertoad
Copy link
Author

@sheremet-va It's perfectly fine to expect user to register their own unhandedRejection handler, key problem is that it doesn't seem possible at the moment, vitest reports the test as failed anyway

@sheremet-va sheremet-va added p3-minor-bug An edge case that only affects very specific usage (priority) and removed enhancement: pending triage labels Jun 23, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
p3-minor-bug An edge case that only affects very specific usage (priority)
Projects
None yet
Development

No branches or pull requests

3 participants