jest icon indicating copy to clipboard operation
jest copied to clipboard

[Feature]: add `expect(spy).toHaveResolvedWith` for spies on async functions

Open plbstl opened this issue 1 year ago • 20 comments

🚀 Feature Proposal

Add a matcher for spies that spy on async functions.

Motivation

When spying on async functions, the received value of expect(spy).toHaveReturnedWith is always a Promise. This causes the tests to fail.

One way to go around this is to manually retrieve the Promise from the mock's metadata and await it.

See: https://github.com/plbstl/minimal-reproduction-jest-expect-tohavereturnedwith/blob/8e070ed6fb3757f5cf36a0550e1d6a5a31bebfd3/tests/index.test.ts#L14

It would be nice if expect(spy).toHaveReturnedWith automatically handles this or a new matcher (for example, toHaveResolvedWith) is created specifically for this use case.

Example

expect(asyncFunctionSpy).toHaveResolvedWith('expected')

Pitch

As helpful as the toHaveReturnedWith matcher when dealing with async code.

plbstl avatar Jul 15 '24 03:07 plbstl

This issue is stale because it has been open 30 days with no activity. Remove stale label or comment or this will be closed in 30 days.

github-actions[bot] avatar Aug 14 '24 04:08 github-actions[bot]

...

plbstl avatar Aug 15 '24 20:08 plbstl

This issue is stale because it has been open 30 days with no activity. Remove stale label or comment or this will be closed in 30 days.

github-actions[bot] avatar Sep 14 '24 20:09 github-actions[bot]

plbstl avatar Sep 14 '24 20:09 plbstl

This issue is stale because it has been open 30 days with no activity. Remove stale label or comment or this will be closed in 30 days.

github-actions[bot] avatar Oct 14 '24 21:10 github-actions[bot]

...

plbstl avatar Oct 27 '24 10:10 plbstl

This issue is stale because it has been open 30 days with no activity. Remove stale label or comment or this will be closed in 30 days.

github-actions[bot] avatar Nov 26 '24 12:11 github-actions[bot]

plbstl avatar Nov 28 '24 12:11 plbstl

This issue is stale because it has been open 30 days with no activity. Remove stale label or comment or this will be closed in 30 days.

github-actions[bot] avatar Dec 28 '24 14:12 github-actions[bot]

a new matcher (for example, toHaveResolvedWith) is created specifically for this use case

And toHaveRejectedWith for parity


Honestly, I was surprised by the lack of these matchers. The workaround for not having a toHaveResolvedWith is currently:

// If all is well, this should be a Promise
const results = mockSomeOtherWork.mock.results[0].value;

// If that Promise resolves to the right stuff, gucci!
const expected = [my, array, of, expected, values];
await expect(results).resolves.toStrictEqual(expected); // passes correctly

// This should fail, because the promise resolved to `expected`
const notExpected = [something, else];
await expect(results).resolves.toStrictEqual(notExpected); // fails correctly

(https://stackoverflow.com/a/62779249/2928233)

amitbeck avatar Jan 09 '25 17:01 amitbeck

This issue is stale because it has been open 30 days with no activity. Remove stale label or comment or this will be closed in 30 days.

github-actions[bot] avatar Feb 08 '25 19:02 github-actions[bot]

🙃

amitbeck avatar Feb 12 '25 01:02 amitbeck

This issue is stale because it has been open 30 days with no activity. Remove stale label or comment or this will be closed in 30 days.

github-actions[bot] avatar Mar 14 '25 01:03 github-actions[bot]

🫠

amitbeck avatar Mar 14 '25 04:03 amitbeck

This issue is stale because it has been open 30 days with no activity. Remove stale label or comment or this will be closed in 30 days.

github-actions[bot] avatar Apr 13 '25 05:04 github-actions[bot]

plbstl avatar Apr 14 '25 11:04 plbstl

+1 for toHaveResolvedWith and toHaveRejectedWith.

ShadowBelmolve avatar Apr 29 '25 19:04 ShadowBelmolve

I also second this feature suggestion.

There is at least one StackOverflow Q/A related to this: https://stackoverflow.com/a/62779249/636849

Lucas-C avatar May 15 '25 10:05 Lucas-C

This issue is stale because it has been open 30 days with no activity. Remove stale label or comment or this will be closed in 30 days.

github-actions[bot] avatar Jun 14 '25 11:06 github-actions[bot]

I was taking a look at the code of toHaveReturnedWith and it does not seem so hard to implement.

With a little luck I should be able to implement at least a basic version of it until the next weekend.

ShadowBelmolve avatar Jun 15 '25 17:06 ShadowBelmolve

This issue is stale because it has been open 30 days with no activity. Remove stale label or comment or this will be closed in 30 days.

github-actions[bot] avatar Jul 15 '25 17:07 github-actions[bot]

I started coding some matchers, curently toHaveResolved, toHaveRejected and toHaveResolvedTimes are finished.

Image

I am thinking about following jest and implementing toHaveX, toHaveXTimes, toHaveXWith, toHaveLastXWith and toHaveNthXWith for Resolved and Rejected.

With some luck it should be finished by next week.

ShadowBelmolve avatar Jul 17 '25 22:07 ShadowBelmolve

This issue is stale because it has been open 30 days with no activity. Remove stale label or comment or this will be closed in 30 days.

github-actions[bot] avatar Aug 17 '25 00:08 github-actions[bot]

lol, almost finished, just making some tests and I will publish.

ShadowBelmolve avatar Sep 04 '25 18:09 ShadowBelmolve

This issue is stale because it has been open 30 days with no activity. Remove stale label or comment or this will be closed in 30 days.

github-actions[bot] avatar Oct 04 '25 19:10 github-actions[bot]

plbstl avatar Oct 08 '25 13:10 plbstl

This issue is stale because it has been open 30 days with no activity. Remove stale label or comment or this will be closed in 30 days.

github-actions[bot] avatar Nov 07 '25 15:11 github-actions[bot]