Skip to content

[Bug]: jest.restoreAllMocks(); breaks global mock #13969

@akaNightmare

Description

@akaNightmare

Version

29.4.3

Steps to reproduce

const client = {
    getConfigurationItems: jest.fn().mockImplementation(async () => [1, 2, 3, 4]),
};
jest.mock('@package/some-lib', () => ({
    ...jest.requireActual('@package/some-lib'),
    getClient: jest.fn().mockImplementation(() => client),
}));

import someLib from '@package/some-lib';

describe('Describe', () => {
  afterEach(() => {
    jest.restoreAllMocks(); // If comment this line everything is working as expected
  });

  it('test1', () => {
    const client = someLib.getClient();
    await client.getConfigurationItems();
    expect(someLib.getClient).toHaveBeenCalled(); // Passed for the first time
  });

  it('test2', () => {
    const client = someLib.getClient();
    await client.getConfigurationItems();
    expect(someLib.getClient).toHaveBeenCalled(); // Failed
  });
});

Expected behavior

I expect to see passed tests as expected. It works for jest 28v

Actual behavior

See error

Error: expect(jest.fn()).toHaveBeenCalled()

Expected number of calls: >= 1
Received number of calls:    0

Additional context

No response

Environment

System:
    OS: macOS 13.0
    CPU: (10) arm64 Apple M1 Pro
  Binaries:
    Node: 18.14.2 - ~/.nvm/versions/node/v18.14.2/bin/node
    npm: 9.5.0 - ~/.nvm/versions/node/v18.14.2/bin/npm
  npmPackages:
    jest: ^29.4.3 => 29.4.3

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions