question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

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:closed
  • Created 5 years ago
  • Reactions:5
  • Comments:17

github_iconTop GitHub Comments

1reaction
yannbrianconcommented, Oct 24, 2022

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.

1reaction
AoDevcommented, Dec 2, 2018

Hello, maybe it’s also related to this issue.

The error I get when using mockResolvedValue is:

TypeError: Cannot read property 'catch' of undefined

I have an async utility to be able to handle individual errors in a Promise.all. It looks like this:

/**
 * Return both failed and successful promises
 * @param {Array.<Promise>} promises
 */
function promiseAllResults (promises) {
  return Promise.all(promises.map((promise) => promise.catch((err) => err)))
}

If I use a “real promise” as a mock, it works. (but then I can not spy)

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found