Skip to content

Mock not called: expected interaction did not happen

Quick summary

Your test expects a mock to be called but the code path did not trigger the mocked function.

Why this happens

  • The code under test takes a different branch that does not call the mocked function

  • The mock was set up on the wrong object or method name

  • The function was called indirectly through a different reference that was not mocked

Minimal example

✗ Broken code
const sendEmail = jest.fn();
registerUser({ email: 'a@b.com', skipWelcome: true });
expect(sendEmail).toHaveBeenCalled(); // fails — skipWelcome skips the email
✓ Fixed code
const sendEmail = jest.fn();
registerUser({ email: 'a@b.com', skipWelcome: false });
expect(sendEmail).toHaveBeenCalled(); // passes — email is sent

Using the correct test input that triggers the code path calling the mock ensures the assertion passes.

How to diagnose

  • Check the mock setup to verify it replaces the correct function on the correct object

  • Add console.log or use mock.calls to inspect what was actually called

  • Review the code under test to trace which code path is executed

How to fix

  • Fix the mock to replace the correct method on the correct object or module

  • Adjust the test expectations to match the actual code path taken

  • Ensure the test input triggers the code path that calls the mocked function

How to prevent

  • Verify mock setup with a simple call before writing complex assertions

  • Use dependency injection to make mocking straightforward and avoid module-level mocks

Related Resources

Related Glossary

Related Lessons

← Back to language