-
-
Notifications
You must be signed in to change notification settings - Fork 6.6k
Closed as not planned
Labels
Description
Version
29.5.0
Steps to reproduce
- Open this repl: https://replit.com/@felash/jest-issue
- click Run
- Notice that the test fails
For convenience, here is the full code:
jest.mock("./add", () => jest.fn(() => 42));
const add = require('./add');
describe('add', () => {
it('should add two numbers', () => {
expect(add(1, 2)).toBe(42);
});
});with
resetMocks: true,
Expected behavior
The test should run fine.
Actual behavior
In this test we create a module mock using jest.mock where we use a jest.fn() mock with an implementation. But we also configured resetMocks in the jest config file. As a result the jest.fn() mock is reset before the test is run (because it's run in beforeEach).
I think that the mock function should be run again before each test, after mocks have been reset.
Additional context
I filed this in #7573 some time ago but that issue got closed for inactivity.
Note: because of #9896, in our project we call resetAllMocks in afterEach. Then this issue would appear at the second test only. But it would still happen.
A workaround is:
jest.mock("./add")
const add = require('./add');
beforeEach((() => {
add.mockImplementation(() => 42);
});
describe('add', () => {
it('should add two numbers', () => {
expect(add(1, 2)).toBe(42);
});
});Environment
System:
OS: Linux 5.15 Ubuntu 20.04.2 LTS (Focal Fossa)
CPU: (8) x64 AMD EPYC 7B12
Binaries:
Node: 16.18.1 - /nix/store/0l5yh0rdzibk8arj9c8gzy1570jkc3vf-nodejs-16.18.1/bin/node
npm: 8.19.2 - /nix/store/0l5yh0rdzibk8arj9c8gzy1570jkc3vf-nodejs-16.18.1/bin/npm
npmPackages:
jest: ^29.5.0 => 29.5.0