jest: mockResolvedValue / mockRejectedValue should returns a real Promise

🐛 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 

About this issue

  • Original URL
  • State: closed
  • Created 6 years ago
  • Reactions: 5
  • Comments: 17 (7 by maintainers)

Most upvoted 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:

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)