mockResolvedValue / mockRejectedValue should returns a real Promise
See original GitHub issue🐛 Bug Report
mockResolvedValue
and mockRejectedValue
should return a real Promise testable with toBeInstanceOf
but they don’t.
To Reproduce
it('should returns a Promise', async () => {
const fn = jest.fn().mockResolvedValue(123);
const promise = fn();
expect(promise).toBeInstanceOf(Promise);
const result = await promise;
expect(result).toBe(123);
});
Expected behavior
It should pass The behaviour should be the same as using
const fn = jest.fn().mockReturnValue(Promise.resolve(123));
Run npx envinfo --preset jest
Paste the results here:
npx: installed 1 in 1.878s
System:
OS: macOS High Sierra 10.13.5
CPU: x64 Intel(R) Core(TM) i7-7820HQ CPU @ 2.90GHz
Binaries:
Node: 8.9.3 - ~/.nvm/versions/node/v8.9.3/bin/node
Yarn: 1.7.0 - /usr/local/bin/yarn
npm: 5.7.1 - ~/.nvm/versions/node/v8.9.3/bin/npm
npmPackages:
@types/jest: ^22.2.0 => 22.2.0
jest: ^22.4.3 => 22.4.4
Issue Analytics
- State:
- Created 5 years ago
- Reactions:5
- Comments:17
Top Results From Across the Web
Mock Functions - Jest
You can create a mock function with jest.fn(). If no implementation is given, the mock function will return undefined when invoked.
Read more >Jest mockRejectedValue throws unhandled promise rejection ...
In my case the action is returning a promise witch would get rejected. In the test, I'm calling the action directly and not...
Read more >JavaScript testing #10. Advanced mocking with Jest and ...
fn(), we can call mockResolvedValue and mockRejectedValue on it. We can achieve a different mocked value per test by manipulating the fetchPosts ...
Read more >How to Mock Promise using Jest - tekloon
We can use mockResolvedValue() in order to mock promise that is fulfilled and returning value. While for mocking promise that is rejected, ...
Read more >Top 4 Jest Tricks to Put On Your Cheatsheet - Level Up Coding
Testing promises can be a little confusing if you're not familiar with Jest's ... and then chain it using mockResolvedValue to set a...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
I agree with @mhombach I struggled with tests for some time to end up discovering this issue.
Would be good to fix it or remove it completely.
Hello, maybe it’s also related to this issue.
The error I get when using
mockResolvedValue
is:I have an async utility to be able to handle individual errors in a Promise.all. It looks like this:
If I use a “real promise” as a mock, it works. (but then I can not spy)