Skip to content

Over-mocking: test tests the mock not the code

Quick summary

Your test mocks so much that it tests the mock setup rather than the actual code under test.

Why this happens

  • Every dependency is mocked, leaving nothing real to test

  • Mocks return hardcoded values that always match assertions regardless of the code

  • The mock is so detailed that it essentially reimplements the real dependency

Minimal example

✗ Broken code
test('processes order', () => {
  const mockDb = { get: jest.fn().mockReturnValue({ price: 100 }) };
  const mockCalc = jest.fn().mockReturnValue(100);
  const mockLog = jest.fn();
  // everything is mocked — test just verifies mock setup
  expect(mockCalc()).toBe(100);
});
✓ Fixed code
test('processes order', () => {
  const mockDb = { get: jest.fn().mockReturnValue({ price: 100 }) };
  const result = processOrder(mockDb, 1); // real processOrder logic
  expect(result.total).toBe(100);
});

Mocking only the database and using the real processOrder function tests the actual business logic, not just the mock.

How to diagnose

  • Check if the test would pass even if the code under test were broken — if so, it tests the mock

  • Review the mock setup to see if it is more complex than the code being tested

  • Try removing some mocks and see if the test still provides meaningful coverage

How to fix

  • Reduce mocking to only external dependencies (network, database) and use real implementations for internal logic

  • Use integration tests with real dependencies for critical paths instead of heavy mocking

  • Simplify mocks to return minimal data needed for the test, not complete reimplementations

How to prevent

  • Mock only external boundaries (I/O, network, database) and test internal logic with real code

  • Prefer integration tests over heavily mocked unit tests for complex business logic

Related Resources

Related Glossary

Related Lessons

← Back to language