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

The Integration Test Sweet Spot

Balance unit and E2E tests by writing integration tests that mock only the boundaries of your system.

The Integration Test Sweet Spot

You'll learn how to balance your testing strategy by writing integration tests that mock only system boundaries, not internal React code.

The Testing Pyramid Problem

Most teams know about the testing pyramid: lots of unit tests, some integration tests, fewer E2E tests. But in React apps, pure unit tests (testing components in complete isolation) create brittle test suites that break during refactors, while full E2E tests are slow and flaky.

The sweet spot is integration testing: rendering real React components together, letting them interact naturally, but mocking only the boundaries of your system.

What Are System Boundaries?

Think of your app as a house. The boundaries are the doors and windows—the places where your code interacts with the outside world:

  • API calls (mock with MSW, not individual fetch functions)
  • Third-party services (analytics, payment processors)
  • Browser APIs (localStorage, geolocation)
  • Environment configuration (feature flags, API URLs)

Everything inside your app—components, hooks, context, routing—should run for real in tests.

Why This Works

When you render a <UserProfile> that fetches data, includes a <UserAvatar> child component, uses useAuth() hook, and accesses router context, don't mock any of that React code. Only mock the API request with MSW.

This approach gives you:

  • Confidence: Multiple units work together correctly
  • Speed: Faster than E2E, no browser needed
  • Stability: Refactoring internal code doesn't break tests
  • Realism: Tests match actual user workflows

Key Takeaway: Integration tests that mock only external boundaries—not React internals—give you the best balance of confidence, speed, and maintainability in your test suite.