jest: [Bug]: "mockImplementation" is behaving different than the syntacical-sugar version"mockResolvedValue"

Version

27.5.1

Steps to reproduce

Repro:

const testMock = {
    works: jest.fn().mockImplementation(() => Promise.resolve(undefined)),
    fails: jest.fn().mockResolvedValue(undefined),
};

it('test-mockResolvedValue', fakeAsync(async() => {
    await testMock.fails();
    flush();
    expect(true).toBeTrue();
}));

it('test-mockImplementation', fakeAsync(async() => {
    await testMock.works();
    flush();
    expect(true).toBeTrue();
}));

Expected behavior

Both tests are passing without any error, since both should be exactly the same as the Jest-docs are stating that mockFn.mockResolvedValue(value) is “Syntactic sugar function for” jest.fn().mockImplementation(() => Promise.resolve(value)).

Actual behavior

Test test-mockResolvedValue fails on the flush() due to The code should be running in the fakeAsync zone to call this function: image

Additional context

I feel this problem is linked to https://github.com/facebook/jest/issues/6645 and https://github.com/facebook/jest/issues/11146 .

Environment

System:
    OS: Windows 10 10.0.19043
    CPU: (12) x64 Intel(R) Core(TM) i7-10850H CPU @ 2.70GHz
Binaries:
    Node: 16.16.0 - C:\Program Files\nodejs\node.EXE
    npm: 8.11.0 - C:\Program Files\nodejs\npm.CMD
npmPackages:
    jest: ^27.2.3 => 27.5.1

About this issue

  • Original URL
  • State: closed
  • Created 2 years ago
  • Reactions: 2
  • Comments: 37 (3 by maintainers)

Most upvoted comments

I can now confirm it has to do with https://github.com/facebook/jest/issues/6645 :

const testMock = {
    works: jest.fn().mockImplementation(() => Promise.resolve(undefined)),
    fails: jest.fn().mockResolvedValue(undefined),
};

it('test-mockImplementation', fakeAsync(async() => {
    console.log(new Promise(() => null) instanceof Promise); // true
    console.log(testMock.works() instanceof Promise); // true
    console.log(testMock.fails() instanceof Promise); // FALSE
    expect(true).toBeTrue();
}));

The mockResolvedValue() does NOT return a normal/real Promise, which buggs out ngZone.

Issue still needs a fix.

I confirm this should be fixed. This is very hard to debug as nothing indicates mockResolved or mockReject could have a different behavior.

For example, waiting for a next tick would finish the promises with mockImplementation but not with mockResolved or mockReject.

Any chance this issue will be verified and tackled? It’s an easy reproducible bug which should be also somewhat-eaasy to fix.

Issue is not fixed yet. Seriously, this issue is now open for almost 1 full year and it’s ignored?

Issue is not fixed yet. Seriously, this issue is now open for over half a year and it’s ignored?