Open plbstl opened 1 month ago
Add a matcher for spies that spy on async functions.
When spying on async functions, the received value of expect(spy).toHaveReturnedWith is always a Promise. This causes the tests to fail.
expect(spy).toHaveReturnedWith
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.
expect(asyncFunctionSpy).toHaveResolvedWith('expected')
As helpful as the toHaveReturnedWith matcher when dealing with async code.
toHaveReturnedWith
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.
...
🚀 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
Pitch
As helpful as the
toHaveReturnedWith
matcher when dealing with async code.