jest-extended icon indicating copy to clipboard operation
jest-extended copied to clipboard

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

Open Guardiannw opened this issue 6 years ago β€’ 8 comments

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 facebook/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 facebook/jest#5919 adds support for async matchers, so this should be much more feasible now.

Guardiannw avatar May 24 '18 15:05 Guardiannw

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?)

benjaminkay93 avatar Sep 07 '18 13:09 benjaminkay93

@mattphillips ping πŸ™‚

SimenB avatar Sep 13 '18 07:09 SimenB

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.

mattphillips avatar Sep 13 '18 11:09 mattphillips

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

benjaminkay93 avatar Sep 13 '18 16:09 benjaminkay93

@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 :smile:

mattphillips avatar Sep 13 '18 16:09 mattphillips

sweet cheers matt, enjoy your hols! πŸ–

benjaminkay93 avatar Sep 13 '18 16:09 benjaminkay93

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

Morikko avatar Oct 29 '18 10:10 Morikko

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()

kael89 avatar Mar 21 '22 08:03 kael89