Skip to content

Add matchers for asserting that a promise has resolved or rejected (regardless of value) #130

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

Open
Guardiannw opened this issue May 24, 2018 · 8 comments

Comments

@Guardiannw
Copy link

Guardiannw commented May 24, 2018

Feature Request

Description:
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. Previous issue referenced at jestjs/jest#5248

Possible solution:
I would like a matcher like expect(promise).toResolve() or expect(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. @SimenB said that jestjs/jest#5919 adds support for async matchers, so this should be much more feasible now.

@benjaminkay93
Copy link
Contributor

This has been sat here for a while, tbh I'm not sure how to go about this, nor how I feel about it either.
I say this because I'm on 10% time at work and figured I'm free enough to kick-start this conversation (hopefully).
https://github.com/mattphillips/babel-jest-assertions is potentially a solution, as this enforces the expect to be called in the advent of a none existent resolve/reject.

I was thinking can we tie into the timeout for a test in the jest config and expect it to be completed by the time that timeout comes, (i think 5 seconds default?)

@SimenB
Copy link
Member

SimenB commented Sep 13, 2018

@mattphillips ping 🙂

@mattphillips
Copy link
Member

Hey guys sorry for the delay in coming back to this, I’m currently holidaying and AFK for the most-part.

@benjaminkay93 I think this should be possible with the new async matchers. I’d imagine it would be a similar implementation to Jest’s .resolves API.

@benjaminkay93
Copy link
Contributor

okay, im happy to give this a go if nobody else wants to, are there any examples of async custom matchers knockin about? would make life easier, ill ofc have a google too

@mattphillips
Copy link
Member

@benjaminkay93 check out the expect extend docs there is an async section

https://jestjs.io/docs/en/expect#expectextendmatchers

Let me know how you get on with this, if you need a hand I’ll be back from my travels in about a week so can look into this too then 😄

@benjaminkay93
Copy link
Contributor

sweet cheers matt, enjoy your hols! 🏖

@Morikko
Copy link

Morikko commented Oct 29, 2018

Would it be possible to return the error message when toResolve() fails ?
Now, it is quite hard to debug without running extra manual tests.

Note: resolves.not.toThrow() works

@kael89
Copy link

kael89 commented Mar 21, 2022

Hi there, congratulations for this awesome library! I was just browsing through this issue, and I have the impression it has since been implemented? Check .toResolve()/.toReject()

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants