Proposal: Revert override of jest default `resetMocks`
See original GitHub issueAbout
Hey what’s up!
I found myself trying to work out why all my tests broke when upgrading react-scripts and then realized it’s because of the change to enabling resetMocks
by default.
I wanted to challenge this decision because, especially when react-scripts is targeted at newer users looking to use react, this causes fundamental jest examples online to break.
App-wide mocks
It also means app-wide mocks such as these don’t work.
// some-mocked-module.js
export const useSomeHook = jest.fn(() => true);
// setupTests.js
jest.mock('some-mocked-module', () => ({
useSomeHook: jest.fn(() => true)
}))
For new users, I can see why it would be very confusing.
Issue Analytics
- State:
- Created 3 years ago
- Reactions:83
- Comments:15 (3 by maintainers)
Top Results From Across the Web
Mocks broken after updating to Jest 26 - Stack Overflow
Jest 26 changed the default behavior of resetMocks to true, which resets mock state before each test. You can revert to the prior...
Read more >How to automatically reset mocks and restore spies in Jest
Configuring Jest to automatically reset / restore mocks and spies · “clearMocks”: true : resets all the mocks usage data, but keeps the...
Read more >Jest CLI Options
Run all tests (default): ... jest --watch #runs jest -o by default ... Optionally pass <boolean> to override option set in configuration.
Read more >Updating React to version 17 - Medium
react-scripts jest config has a breaking change: resetMocks is true by default and also causes mocks to reset in nested describe . To...
Read more >Jest-fetch-mock: Jest Mock for Fetch - Morioh
resetMocks () will return to the default behavior of mocking all fetches with a text response of empty string. fetch.dontMock() - Change the...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Resetting a mock will reset it’s mock implementation, this could make it confusing if you tried to do this:
With
resetMocks
that mock implementation will only work for the first test, then it will get reset. The fix for this would be to require folks to do this:That works, but it can be confusing.
However, there are great reasons to get the mocks cleared between runs. So I think I’d suggest using
clearMocks: true
instead ofresetMocks: true
. The only big difference is that the mock implementation remains. This comes with its own set of trade-offs though because it means that if I do this:With that context, I’m not certain which to suggest. I think
resetMocks
is probably the best between the two, but there are foot guns for both.Last thing I’d say is that we should configure at least one of them. You never want to have the mock state preserved between tests. So it should at least do
clearMocks
.@k-yle Are you sure? Adding this to
package.json
solved it for us using CRA 4.0.1: