Mock Not Called
Error message
Expected mock function to have been called, but it was not called
Quick summary
The assertion toHaveBeenCalled() failed because the mock was never called. Your code path didn't trigger the expected function call.
Why this happens
The code under test took a different branch that doesn't call the mocked function.
The mock was set up on the wrong object or method name.
The function was called before the mock was set up.
The test inputs didn't trigger the code path that calls the function.
Minimal example
const mockFn = jest.fn();
test('calls callback', () => {
// Code doesn't call mockFn because condition is false
processData([], mockFn);
expect(mockFn).toHaveBeenCalled(); // fails
});const mockFn = jest.fn();
test('calls callback', () => {
// Provide data that triggers the callback
processData([1, 2, 3], mockFn);
expect(mockFn).toHaveBeenCalled(); // passes
});Provide test inputs that trigger the code path calling the mock function.
How to diagnose
Check if the mock is set up on the correct object and method name.
Trace through the code to see which branches are taken with the test inputs.
Add console.log in the code to see if the function is reached.
Check if the mock is set up before the code under test runs.
How to fix
Fix the test inputs to trigger the code path that calls the function.
Set up the mock before calling the code under test.
Verify the mock is on the correct object — check spelling and module paths.
If the code shouldn't call the function in this scenario, remove the assertion.
How to prevent
Set up mocks in beforeEach to ensure they're ready before each test.
Use clear test data that deterministically triggers the expected code paths.