What Makes a Good RTL Test
You'll learn the three hallmarks of a reliable, maintainable React Testing Library test.
The Three Pillars of Good RTL Tests
A good RTL test mirrors how a real user would interact with your component. This means three things work together:
1. Query by Role or Label (What Users See)
Real users don't look for data-testid attributes or CSS classes. They see a button labeled "Submit" or a textbox with a "Username" label. Your tests should too:
// ❌ Bad: Implementation detail
const button = container.querySelector('.submit-btn');
// ✅ Good: What the user sees
const button = screen.getByRole('button', { name: /submit/i });
2. Simulate Real Events (What Users Do)
Users click, type, and interact through the browser. Use userEvent to match this behavior, not artificial shortcuts:
// ❌ Bad: Direct state manipulation
fireEvent.change(input, { target: { value: 'test' } });
// ✅ Good: Simulates real typing
await userEvent.type(input, 'test');
3. Assert Visible Changes (What Users Notice)
Test what appears on screen, not internal state or props. If a user can't see it, they can't verify it:
// ❌ Bad: Testing implementation
expect(component.state.isLoading).toBe(false);
// ✅ Good: Testing visible result
expect(screen.getByText(/welcome back/i)).toBeInTheDocument();
The Analogy
Think of your test as a QA tester using your app with their eyes and mouse—they can't peek at the code, only what's rendered and how it responds.
Key Takeaway: Good RTL tests query by accessible roles/labels, simulate realistic user events, and assert only the visible changes users would actually notice—creating tests that resist refactoring and catch real bugs.