Don't Mock Child Components
You'll learn: Why shallow rendering and mocking child components undermines test confidence and how to test the full component tree instead.
The Integration Testing Mindset
When you mock child components in your tests, you're essentially replacing real parts of your component tree with fake placeholders. It's like test-driving a car but replacing the wheels with wooden blocks—you're no longer testing how the parts work together.
Shallow rendering tools (like Enzyme's shallow) or manually mocking child components feels convenient: tests run faster, you avoid "unrelated" failures. But here's the problem: you're testing a version of your component that never actually runs in production.
Why Full Component Trees Matter
Your users interact with the entire component hierarchy. When a parent passes props to a child, and that child conditionally renders based on those props, that's real integration that needs testing. Mocking the child means you never verify:
- Props are passed correctly
- The child actually renders what you expect
- Event callbacks flow back up properly
- Context providers reach nested children
Instead of mocking <UserCard /> when testing <UserList />, render the real <UserCard />. Use MSW to control API data, not component mocks to control rendering.
The Exception
The only time you might mock a child component is when it's a third-party component with heavy external dependencies (like a map library requiring browser APIs unavailable in tests). Even then, prefer integration-testing the real thing when possible.
Key Takeaway: Test your components as they run in production—with real children, real props flow, and real integration. Mock external APIs with MSW, not internal React components.