Jest Expect Call Count Mismatch Fix
In this tutorial, you'll learn about Jest Expect Call Count Mismatch Fix. We cover key concepts, practical examples, and best practices.
Your expect(mock).toHaveBeenCalledTimes(n) assertion fails — the mock was called fewer or more times than expected, even though you think it should match.
The Problem
// WRONG — mocks accumulate calls across tests
const mockFn = jest.fn();
test('test 1', () => {
mockFn();
expect(mockFn).toHaveBeenCalledTimes(1); // Passes
});
test('test 2', () => {
mockFn();
expect(mockFn).toHaveBeenCalledTimes(1); // FAILS — already called once in previous test
});
Expected number of calls: 1
Received number of calls: 2
Mock functions accumulate call counts across tests unless reset. Test 2 inherits the call from Test 1.
Step-by-Step Fix
1. Clear mocks between tests
// RIGHT — reset call counts
beforeEach(() => {
jest.clearAllMocks();
});
const mockFn = jest.fn();
test('test 1', () => {
mockFn();
expect(mockFn).toHaveBeenCalledTimes(1);
});
test('test 2', () => {
mockFn();
expect(mockFn).toHaveBeenCalledTimes(1); // Now passes
});
2. Check individual calls with toHaveBeenNthCalledWith
// RIGHT — verify specific call arguments
const mockFn = jest.fn();
mockFn('first call');
mockFn('second call');
mockFn('third call');
expect(mockFn).toHaveBeenCalledTimes(3);
expect(mockFn).toHaveBeenNthCalledWith(1, 'first call');
expect(mockFn).toHaveBeenNthCalledWith(2, 'second call');
expect(mockFn).toHaveBeenNthCalledWith(3, 'third call');
3. Use toHaveBeenCalledWith for individual verification
// RIGHT — check if ever called with specific args
mockFn('hello', 1);
mockFn('world', 2);
expect(mockFn).toHaveBeenCalledWith('hello', 1);
expect(mockFn).toHaveBeenCalledWith('world', 2);
expect(mockFn).toHaveBeenCalledTimes(2);
4. Debug call count failures
// RIGHT — inspect calls for debugging
mockFn('a');
mockFn('b');
console.log(mockFn.mock.calls);
// [['a'], ['b']]
console.log(mockFn.mock.calls.length);
// 2
Expected output:
PASS tests/calls.test.js
✓ test 1 (4 ms)
✓ test 2 (3 ms)
✓ verifies specific calls (5 ms)
Prevention Tips
- Always call
jest.clearAllMocks()inbeforeEach - Use
jest.resetAllMocks()to also clear implementations - Use
jest.restoreAllMocks()for spies - Check
mockFn.mock.callsfor debugging - Use
toHaveBeenCalledTimes(0)to verify a mock was never called
Common Mistakes with expect call times
- Forgetting that lazy evaluation defers computation until the value is forced, causing space leaks with unevaluated thunks
- Using
returnto exit a function early instead of wrapping a pure value in the monad - Mixing let bindings with <- bindings in do notation, producing type errors
These mistakes appear frequently in real-world JEST code. DodaTech's contributors have identified these patterns through analysis of open-source projects and production systems.
Practice Exercise
Write a pure function that safely divides two integers using Maybe, then test it with edge cases like division by zero and negative numbers.
This exercise reinforces the concepts covered in this guide. Try implementing it before checking online solutions.
FAQ
Built by the developers of DodaTech
Doda Browser, DodaZIP & Durga Antivirus Pro