Reset Jest mock functions calls count before every test
Mock functions are a powerful feature in Jest that allows you to spy on function calls and replace their implementations for testing purposes. However, it’s sometimes crucial to reset these mocks before every test to ensure that each test runs in isolation and remains unaffected by any previous test. This guide will walk you through the steps to reset Jest mock functions calls count before every test.
Why reset mocks?
Before delving into the “how”, it’s essential to understand the “why”. If you do not reset your mock call count, subsequent tests might be affected by the previous tests behavior. This can lead to false positives or negatives, making your tests unreliable. Resetting ensures each test starts with a clean slate.
Using beforeEach
to reset mocks
Jest provides lifecycle methods like beforeEach
that run at specific times during your tests. You can use beforeEach
to reset mock functions before each test run.
let myMockFunction
beforeEach(() => {
myMockFunction = jest.fn()
})
test('first test', () => {
myMockFunction()
expect(myMockFunction).toHaveBeenCalledTimes(1)
})
test('second test', () => {
expect(myMockFunction).toHaveBeenCalledTimes(0) // This passes because we reset the mock in `beforeEach`.
})
Using mockClear
If you have globally defined mock functions, and you just want to reset their usage data without resetting their implementation, you can use mockClear
.
const myMockFunction = jest.fn(() => 'mocked value')
beforeEach(() => {
myMockFunction.mockClear()
})
Using jest.clearAllMocks
While mockClear
is used to reset the state of a specific mock function, sometimes you may have multiple mock functions in your test suite and want to clear all of them at once. Jest provides a method for this called jest.clearAllMocks
.
This method will clear the call counts, instances, and returned values for all mock functions in your Jest environment. It’s particularly useful if you’re unsure about the total number of mocks, or if you just want to ensure that every mock is reset without listing each one explicitly.
const mockFunctionA = jest.fn()
const mockFunctionB = jest.fn()
beforeEach(() => {
jest.clearAllMocks()
})
test('test using mockFunctionA', () => {
mockFunctionA()
expect(mockFunctionA).toHaveBeenCalledTimes(1)
expect(mockFunctionB).toHaveBeenCalledTimes(0) // Not affected by any previous tests because we cleared all mocks.
})
Using jest.clearAllMocks
ensures that all your mocks start with a clean slate before every test, providing more confidence in your test suite reliability.
Automatic mock reset
Jest also provides configuration options to automate mock resets:
resetMocks
: Resets the state (including calls count) of all mocks between tests. If this option is set totrue
in your Jest configuration, you won’t need to reset mocks manually.
{
"resetMocks": true
}
clearMocks
: Clears the call count of all mock functions between tests. It’s similar toresetMocks
, but it doesn’t reset their implementation.
{
"clearMocks": true
}
Conclusion
Resetting mock functions in Jest before every test ensures your tests remain accurate and isolated. Whether you choose to reset them manually using lifecycle methods, or automatically using Jest configuration, it can be helpful to ensure that your tests are not affected by previous mock calls.
Happy testing!