-
-
Notifications
You must be signed in to change notification settings - Fork 6.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[Bug]: Module mocks should be called again for each test #14080
Comments
I think that everything here works as expected in most predictable manner. The "workaround" sample does not look like a workaround. I think this is the actual way you should setup mocks if you prefer to have As you noticed Jest is resetting mocks using From your #9896 (comment)
Why? Not sure what would that change?
Exactly! The For instance, if mocks would be reset after each test case the behaviour would be unpredictable. As you noticed "this issue would appear at the second test only." Mark that second test with |
Predictable, yes.
If Jest was running the mock factory again as I'm suggesting here, this would still be possible.
This issue isn't about that, let's focus on the problem described in this issue please. |
Interesting, but what if someone might want to op-out? Currently Sorry for bringing in comments from another issue. I misunderstood, it felt like you are opening this issue, because that one got closed. |
This issue is stale because it has been open 30 days with no activity. Remove stale label or comment or this will be closed in 30 days. |
up |
This issue is stale because it has been open 30 days with no activity. Remove stale label or comment or this will be closed in 30 days. |
up |
This issue is stale because it has been open 30 days with no activity. Remove stale label or comment or this will be closed in 30 days. |
up |
I'm running into this too after upgrading To me, when I run Looking at it another way, I can't think of a situation where the current behavior is desirable. The |
This issue is stale because it has been open 30 days with no activity. Remove stale label or comment or this will be closed in 30 days. |
up |
What you mention is IMO another issue which has been fixed in 29.6.3, see #14429. |
This issue is stale because it has been open 30 days with no activity. Remove stale label or comment or this will be closed in 30 days. |
This issue was closed because it has been stalled for 30 days with no activity. Please open a new issue if the issue is still relevant, linking to this one. |
Can it be reopened? These bots are painful. |
This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs. |
Version
29.5.0
Steps to reproduce
For convenience, here is the full code:
with
Expected behavior
The test should run fine.
Actual behavior
In this test we create a module mock using
jest.mock
where we use ajest.fn()
mock with an implementation. But we also configuredresetMocks
in the jest config file. As a result thejest.fn()
mock is reset before the test is run (because it's run inbeforeEach
).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
inafterEach
. Then this issue would appear at the second test only. But it would still happen.A workaround is:
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
The text was updated successfully, but these errors were encountered: