Course contentsShow
← React
Lesson 2040 of 2,39646. Testing React with RTLFree lesson

RTL vs Enzyme: Philosophy Differences

Enzyme allowed shallow rendering and state inspection; RTL deliberately removes these to enforce better tests.

RTL vs Enzyme: Philosophy Differences

What you'll learn: How React Testing Library and Enzyme diverged fundamentally in what they let you test—and why RTL's constraints make you a better tester.

The Enzyme Era: Maximum Access

Enzyme, the previous testing standard, gave you direct access to component internals. You could shallow render a component (isolating it from children), inspect its state directly, check prop values, and even spy on lifecycle methods. The philosophy was "give developers every tool to verify their code works."

This sounds great—until you realize it encouraged tests like:

// Enzyme-style (hypothetical)
expect(wrapper.state('isOpen')).toBe(true);
expect(wrapper.find(Modal).prop('visible')).toBe(true);

These tests pass when your code is structured exactly as written, but they don't prove your user can actually see the modal.

RTL's Deliberate Limitations

React Testing Library intentionally removed shallow rendering and state inspection. You cannot check state. You cannot directly access props. You can only interact with the DOM as a user would—click buttons, read text, find labels.

// RTL-style (what you've learned)
expect(screen.getByRole('dialog')).toBeInTheDocument();

This forces you to test the outcome (modal visible in DOM) rather than the mechanism (state variable changed).

The Philosophy Shift

Enzyme: "Test that your code is structured correctly."
RTL: "Test that your component behaves correctly for users."

Enzyme's flexibility became its weakness—it made fragile tests easy to write. RTL's constraints feel restrictive at first, but they guide you toward resilient tests that survive refactors.

Key Takeaway: RTL deliberately removes access to component internals (state, shallow rendering) that Enzyme provided, enforcing tests based on user-observable behavior rather than implementation details—making tests more valuable and less brittle.