jest: [Bug]: mockReset restoring mocks instead of resetting them

Version

29.4.3

Steps to reproduce

Simple test to repro:

describe("mockReset", () => {
  const originalReturnValue = "original";
  const mockReturnValue = "mocked";

  test("spyOn", () => {
    const module = { api: () => originalReturnValue };
    jest.spyOn(module, "api").mockImplementation(() => mockReturnValue);

    expect(module.api()).toStrictEqual(mockReturnValue);
    expect(module.api).toHaveBeenCalledTimes(1);

    module.api.mockReset();
    expect(module.api).toHaveBeenCalledTimes(0);

    expect(module.api()).toStrictEqual(undefined);
  });

  test("overwrite", () => {
    const module = { api: () => originalReturnValue };
    module.api = jest.fn().mockImplementation(() => mockReturnValue);

    expect(module.api()).toStrictEqual(mockReturnValue);
    expect(module.api).toHaveBeenCalledTimes(1);

    module.api.mockReset();
    expect(module.api).toHaveBeenCalledTimes(0);

    expect(module.api()).toStrictEqual(undefined);
  });
});

Expected behavior

The test should pass where module.api() is reset to undefined in both the spyOn and overwrite cases.

Actual behavior

The test fails in spyOn case.

  ● mockReset › spyOn

    expect(received).toStrictEqual(expected) // deep equality

    Expected: undefined
    Received: "original"

      13 |     expect(module.api).toHaveBeenCalledTimes(0);
      14 |
    > 15 |     expect(module.api()).toStrictEqual(undefined);
         |                          ^
      16 |   });

Additional context

The fix for resetAllMocks from https://github.com/facebook/jest/issues/13808 should be applied to mockReset too?

Environment

System:
    OS: macOS 13.1
    CPU: (8) arm64 Apple M1 Pro
  Binaries:
    Node: 18.13.0 - ~/Library/Caches/fnm_multishells/59531_1676389811348/bin/node
    Yarn: 3.4.1 - ~/Library/Caches/fnm_multishells/59531_1676389811348/bin/yarn
    npm: 8.19.3 - ~/Library/Caches/fnm_multishells/59531_1676389811348/bin/npm
  npmPackages:
    jest: ^29.4.3 => 29.4.3

About this issue

  • Original URL
  • State: closed
  • Created a year ago
  • Comments: 15 (8 by maintainers)

Most upvoted comments

Reopening as @shlomo-artlist had commented on how this is a breaking change, and how it makes mockRestore redundant.

https://github.com/facebook/jest/issues/13229#issuecomment-1419418186

The https://github.com/facebook/jest/issues/5143#issuecomment-572024312 which you mentioned is now somewhat outdated. Since Jest v29.4.0 first answer should read:

“Does mockReset() reset implementation for spies?” Yes. The implementation of a mock will be reset to the initial state.

Reference #13692


If your CIs are public, I can take a look. Minimal reproduction is always welcome too.

I’d like to second that 29.4.3 has some breaking changes. It’s probably either of these, but we have not identified which particularly:

We’ve been using restoreMocks: true jest config option since jest version 24 and it worked for us, but with the 29.4.2 -> 29.4.3 update quarter of our tests started to fail. If we flip the option to false, there are much less tests starting to fail (and they fail for a different reason).

Unfortunately, I can’t provide a minimal reproducible example yet. If we will be able to identify the exact problem, I’ll post an update here. I’m making this comment just to see if others have similar issues.

Also, this comment might be related, since replit example from it also doesn’t work with version 29.4.3.

PS. Both of the above changes are announced as “fixes”, so maybe we were relying on a bug in our setup for 5 major versions of jest 😅. Still it does not sound like it should be a patch-version update.