Last updated: Mar 7, 2024
Reading time·6 min
To reset a single mock function, call the mockClear()
method on the function
in the afterEach()
hook.
If you need to reset all mock functions, call the jest.clearAllMocks()
method in the afterEach()
hook.
The method is equivalent to calling .mockClear()
on every mocked function.
Here is an example of resetting a single mock function.
const mockFn = jest.fn(); afterEach(() => { mockFn.mockClear(); }); it('tests that mock function was called', () => { mockFn('bobbyhadz.com'); expect(mockFn).toHaveBeenCalledTimes(1); expect(mockFn).toHaveBeenCalledWith('bobbyhadz.com'); }); it('tests that mock function was NOT called', () => { expect(mockFn).not.toHaveBeenCalled(); });
If I run the tests with npx jest
, I can see that both tests pass.
npx jest
We used the
mockClear() method
to reset the mock in the afterEach()
hook.
The mockClear()
method clears all information stored in:
mockFn.mock.calls
mockFn.mock.instances
mockFn.mock.contexts
mockFn.mock.results
The afterEach()
hook is run after each test.
The first test calls the mock function and checks if it has been called 1 time with a specific argument.
it('tests that mock function was called', () => { mockFn('bobbyhadz.com'); expect(mockFn).toHaveBeenCalledTimes(1); expect(mockFn).toHaveBeenCalledWith('bobbyhadz.com'); });
The second test doesn't call the mock function and asserts that it hasn't been called.
it('tests that mock function was NOT called', () => { expect(mockFn).not.toHaveBeenCalled(); });
If you need to reset all functions between tests when using Jest, call the
jest.clearAllMocks()
method in the afterEach()
hook.
const mockFn = jest.fn(); afterEach(() => { jest.clearAllMocks(); }); it('tests that mock function was called', () => { mockFn('bobbyhadz.com'); expect(mockFn).toHaveBeenCalledTimes(1); expect(mockFn).toHaveBeenCalledWith('bobbyhadz.com'); }); it('tests that mock function was NOT called', () => { expect(mockFn).not.toHaveBeenCalled(); });
The jest.clearAllMocks()
method clears the mock.calls
, mock.instances
, mock.contexts
and
mock.results
properties of all mocks.
Using the method is equivalent to calling mockClear()
on every mocked
function.
mockReset()
There is also a mockReset() method you can use to reset a mock function in Jest.
The method does everything that mockFn.mockClear()
does, however, it also
removes any mocked return values or implementations.
This is useful when you completely want to reset a mock function to its initial state.
const mockFn = jest.fn(); mockFn.mockImplementation(() => ({ id: 1, site: 'bobbyhadz.com', topic: 'jest', })); afterEach(() => { // 👇️ reset all mocks and their return values mockFn.mockReset(); }); it('tests that mock function was called', () => { mockFn('bobbyhadz.com'); expect(mockFn).toHaveBeenCalledTimes(1); expect(mockFn).toHaveBeenCalledWith('bobbyhadz.com'); expect(mockFn).toReturnWith({ id: 1, site: 'bobbyhadz.com', topic: 'jest', }); }); it("tests that mock function's return value is reset", () => { mockFn('google.com'); expect(mockFn).toHaveBeenCalledTimes(1); expect(mockFn).toHaveBeenCalledWith('google.com'); expect(mockFn).toReturnWith(undefined); });
We used the mockReset()
method to reset all mocks and their return values.
The first test in the file tests that the mock function returns the expected object.
The second test tests that the mock function returns undefined
after the
mockReset()
function has removed any mocked return values or implementations.
jest.resetAllMocks()
If you need to reset all mock functions and remove any mocked return values or implementations from all mocks, use the jest.resetAllMocks() method.
afterEach(() => { jest.resetAllMocks() });
Calling the jest.resetAllMocks
method is equivalent to calling mockReset()
on every mocked function.
You can also configure Jest to reset or clear all mocks after each test
automatically in your jest.config.js
file.
If you don't already have a jest.config.js
file, create one in the root
directory of your project (right next to your package.json
) file.
If you want to reset all mock (and their return values), set the following property.
module.exports = { resetMocks: true, };
If you just want to clear all mocks after each test (without removing mocked return values or implementations), set the following property.
module.exports = { clearMocks: true, };
As previously noted, the difference between the two properties is that
resetAllMocks
does everything that clearMocks
does, however, it also removes
any mocked return values or implementations.
If you set either of the two properties, Jest will automatically reset your mocked functions after each test.
Therefore, you no longer have to use the afterEach()
hook as we did in the
previous examples.
You have to pick only one of the two properties above.
However, there is also a restoreMocks
property. The property restores all
mocks and replaced properties back to their original, non-mocked values.
module.exports = { restoreMocks: true, };
Note that the property only works for mocks created with jest.spyOn()
.
If you didn't create the mock using jest.spyOn()
, you manually have to restore
it to its original value.
The restoreMocks
property can be mixed with resetMocks
or clearMocks
.
However, you can't set resetMocks
and clearMocks
to true
in the same
jest.config.js
.
mockRestore()
and jest.restoreAllMocks()
methodsYou can also use the mockRestore() and jest.restoreAllMocks() methods to reset all mocks.
The mockRestore
method does everything that the mockReset
method does but it
also restores the original, non-mocked implementation of the function.
Note that mockRestore()
and jest.restoreAllMocks()
methods only work when
the mock was created using jest.spyOn()
.
If the mock was created using jest.fn()
, you have to restore it manually.
Here is an example that uses the mockRestore
method to restore the original
non-mocked implementation of a function.
const video = { play() { return true; }, }; const videoSpy = jest.spyOn(video, 'play'); videoSpy.mockImplementation(() => false); afterEach(() => { videoSpy.mockRestore(); }); it('tests that spy function was called', () => { const isPlaying = video.play(); expect(videoSpy).toHaveBeenCalled(); expect(isPlaying).toBe(false); }); it('tests that spy function was NOT called', () => { const isPlaying = video.play(); expect(videoSpy).not.toHaveBeenCalled(); expect(isPlaying).toBe(true); });
We used the jest.spyOn()
method to spy on a function and set its return value
from true
to false
using the mockImplementation()
method.
The first test calls the function and asserts that the spy has been called and
the function's return value is false
.
Then the afterEach()
hook runs and restores the non-mocked implementation of
the function.
The second test calls the function and asserts that the spy was not called and
the function's return value is true
.
If you need to restore all mocked functions to their non-mocked implementation,
call the jest.restoreAllMocks()
in the afterEach()
hook.
afterEach(() => { jest.restoreAllMocks(); });
The method is equivalent to calling mockRestore()
on each mocked function.
You can also reset mocks and spies using the command line.
The --clearMocks flag can be set to automatically clear mock calls, instances, contexts and results before each test.
npx jest --clearMocks
Using the --clearAllMocks
flag is equivalent to calling jest.clearAllMocks()
before each test.
Note that the --clearAllMocks
flag does not remove mocked implementations that
have been set.
The --resetMocks flag can be set to automatically reset mock state before every test.
npx jest --resetMocks
This is equivalent to calling jest.resetAllMocks()
before each test.
The --resetMocks
flag will remove the fake implementations of all mocked
functions but it doesn't restore their initial implementation.
The --restoreMocks method automatically restores mock state and implementations before each test.
npx jest --restoreMocks
Setting the flag is equivalent to calling jest.restoreAllMocks()
before each
test.
When the flag is set, all mocks will have their fake implementation removed and their initial implementation will be restored.
Note that this only works for functions that were mocked using jest.spyOn
.
If the mock was created using jest.fn()
, you have to restore it manually.
You can also set the flags in the test
script in your package.json
file.
{ "scripts": { "test": "jest --clearMocks" } }
The next time you issue the npm run test
command, the --clearMocks
flag will
clear mock calls, instances, contexts and results before each test.
You can learn more about the related topics by checking out the following tutorials: