Course contentsShow
← React
Lesson 2125 of 2,39647. Testing Patterns and Best PracticesFree lesson

The Cost of Mocking: False Confidence

Understand how over-mocking creates tests that pass but don't reflect real behavior, hiding integration bugs.

The Cost of Mocking: False Confidence

You'll learn how excessive mocking can make your tests pass while hiding real bugs that would occur in production.

The Problem with Over-Mocking

When you mock too many dependencies in your tests, you create an isolated bubble where everything behaves perfectly—but this bubble doesn't represent reality. Your tests pass because the mocks do exactly what you tell them to, not because your code actually works correctly.

Think of it like testing a car by replacing the engine, wheels, and transmission with cardboard cutouts. Sure, you can verify the steering wheel turns, but you haven't tested whether the car actually drives.

How False Confidence Develops

Imagine testing a component that fetches user data and displays it:

// Over-mocked test - FALSE CONFIDENCE
vi.mock('./api')
vi.mock('./UserDisplay')
vi.mock('./ErrorBoundary')

test('shows user data', () => {
  render(<UserProfile id="123" />)
  expect(screen.getByText('Loading...')).toBeInTheDocument()
  // Passes! But integration is completely untested
})

This test passes, but it doesn't verify that:

  • The API actually returns the right data structure
  • UserDisplay can render that data
  • Error handling works when the API fails
  • The components integrate correctly

The Hidden Integration Bugs

The most dangerous bugs live at the boundaries between components. When you mock everything, those boundaries disappear from your tests. Your code might pass type checks but fail at runtime because the API changed shape, or a component expects different props than you're providing.

Key Takeaway: Mock sparingly—only external dependencies you can't control. Test real integration between your components to catch bugs that mocks would hide.