Add "clearAllMocks" method that will run "mockClear" on all mocks
Closed this issue · 9 comments
Do you want to request a feature or report a bug?
feature
What is the current behavior?
Currently, there is no way to clear all mocks at once. There used to be a method called jest.clearAllMocks
, but it was actually just resetting all the mocks, and appears to have been replaced with jest.resetAllMocks
in 17.0.0 (https://github.com/facebook/jest/blob/224491bf9a51834c2d348e2d0c81b3b970150bd4/CHANGELOG.md).
The problem with resetting all mocks is that it also replaces the implementations that you set on the mock, so you can't call it before/after tests if other tests rely on those implementations. Because of this, you have to call myMock.mockClear()
on every mock between tests, which will only reset the calling information.
If the current behavior is a bug, please provide the steps to reproduce and if possible a minimal repository on GitHub that we can npm install
and npm test
.
N/A
What is the expected behavior?
Since Jest supports both myMock.mockReset()
and myMock.mockClear()
, it would make sense that there would be an equivalent for clearing all mocks, since there is one for reseting them.
Run Jest again with --debug
and provide the full configuration it prints. Please mention your node and npm version and operating system.
N/A
jest 17 broke many of my tests, the following worked on jest 16.0.2 with clearAllMocks
https://github.com/acateland/jest-mock-issue
So i guess clearAllMocks was not just renamed
The above workaround consisting of reseting mock manually doesn't apply to mocked modules
This has come up a few times. I think we need to bring clearAllMocks
back. cc @mrsharpoblunto
Would this also affect timer mocks? I ran into issues where after upgrading to 17 jest.runAllTimers()
wouldn't run setTimeouts when I run resetAllMocks
on beforeEach
block. Same code worked with Jest 16 and clearAllMocks
.
It also makes sense to add clearMocks boolean flag as part of Jest configuration, similar to resetMocks flag that already exists.
We cannot upgrade to Jest 17 because of this :(
This caused me a bit of head scratching today. I mistakenly got the impression from the release notes that resetAllMocks
was a 1:1 replacement for clearAllMocks
. I see that's not the case - clearAllMocks
is actually what I'm looking for.
The differences between these are slightly tricky:
mockClear
: resets call counts etc.
mockReset
: resets all information stored in the mock
Maybe mockClear
could be more explicitly named, e.g mockResetCalls
or something better. Or maybe mockReset({callsOnly: true})
and resetAllMocks({callsOnly: true})
Sorry I don't have a better suggestion at this stage
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.
Please note this issue tracker is not a help forum. We recommend using StackOverflow or our discord channel for questions.