Jest allMocks functions
There are three jest.*AllMocks()
functions which correspond to three mockFn.mock*()
functions, but call them on all mock objects.
All three can be configured to automatically run before each test case.
mockClear()
and clearAllMocks()
These clear the mock.calls
, mock.instances
, mock.contexts
and mock.results
properties.
So you keep your mock implementation intact, only clearing any saved calls and results.
See jest.clearAllMocks()
and mockFn.mockClear()
.
For configuration, see the clearMocks option.
mockReset()
and resetAllMocks()
Does everything that mockClear()
does, and also replaces the mock implementation with an empty function.
That empty function will take in any args, and return undefined
.
See jest.resetAllMocks()
and mockFn.mockReset()
For configuration, see the resetMocks option.
mockRestore()
and restoreAllMocks()
Does everything that mockReset()
does, and also restores the original non-mocked implementation.
WARNING
Under the hood, this is done by calling .restore()
on every replaced property. Meaning, if you replaced properties manually with jest.replaceProperty()
then you will need to restore those manually.
So this will work perfectly for anything made with jest.spyOn()
, but not for manually replaced properties or jest.fn()
s.