-
-
Notifications
You must be signed in to change notification settings - Fork 6.5k
New issue
Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? # to your account
Add support for asserting that a promise has resolved or rejected (regardless of value) #5248
Comments
Does |
No, it doesn't work. |
Hey @SimenB, is there any update on this? Thanks! |
We've landed support for asynchronous matchers, so you can add a @mattphillips something for Closing as I think it can be solved in user land. Happy to reopen if proven wrong |
@Guardiannw I like this! Mind opening this up in jest-extended and I’ll help get this working in user land when Jest@23 lands? 😄 |
@mattphillips Will do! Thanks! |
Just a note that I would find this useful! My use case is that I am testing an abstract class and I want to be able to test that async methods that have been overridden will properly resolve in the context of the parent objects that call them. |
For anyone who wants a simple solution for asserting promise resolution or rejection:
async function toReject(promise) {
let rejected
try {
await promise
rejected = false
} catch {
rejected = true
}
return {
pass: rejected,
message: () => `expected promise to ${rejected ? "resolve" : "reject"}`,
}
}
async function toResolve(promise) {
let resolved
try {
await promise
resolved = true
} catch {
resolved = false
}
return {
pass: resolved,
message: () => `expected promise to ${resolved ? "reject" : "resolve"}`,
}
}
expect.extend({
toReject,
toResolve,
}) Add setup file to your jest config:
module.exports = {
// ...
setupFilesAfterEnv: ["<rootDir>/setupTests.js"],
} Import
import "./extendExpect" Usage: expect(promise).toResolve()
expect(promise).toReject() |
This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs. |
Do you want to request a feature or report a bug?
Feature Request
What is the current behavior?
Currently, there is no declarative and easy way to check if a promise resolves or rejects.
What is the expected behavior?
I would like a way to be able to check if a promise resolves or rejects, regardless of the value that it produces, or if it even produces a value, it is irrelevant. In particular, i want to easily be able to easily check to make sure that a promise was not rejected. I don't care at all if it actually sent an error, undefined, or whatever, I just want to make sure it was not rejected. I would like a matcher like
expect(promise).toResolve()
orexpect(promise).not.toReject()
. Right now, as far as I can tell, there is no simple way to go about asserting whether or not a promise has resolved or rejected without taking into account its value. I want to be able to assert this without regard to its response, the error message, or otherwise.Please provide your exact Jest configuration and mention your Jest, node,
yarn/npm version and operating system.
Jest version 22.0.4
The text was updated successfully, but these errors were encountered: