| name | react-testing-library |
| description | User-centric React component testing. Trigger: When testing React components with RTL. |
| license | Apache 2.0 |
| metadata | {"version":"1.1","type":"tooling","skills":["react","jest","unit-testing"],"dependencies":{"@testing-library/react":">=14.0.0 <15.0.0"}} |
React Testing Library
Tests components the way users interact with them -- querying by accessible roles and text, not implementation details.
When to Use
- Testing React components from the user's perspective
- Simulating user interactions (clicks, typing, forms)
- Writing tests that survive internal refactors
Don't use for:
- Pure function or service logic (use jest skill)
- E2E multi-page flows (use Playwright or Cypress)
Critical Patterns
render + screen Queries
render(<LoginForm />);
const button = screen.getByRole('button', { name: /submit/i });
const { getByRole } = render(<LoginForm />);
userEvent over fireEvent
import userEvent from '@testing-library/user-event';
const user = userEvent.setup();
await user.type(screen.getByRole('textbox', { name: /email/i }), 'ada@test.com');
fireEvent.change(input, { target: { value: 'ada@test.com' } });
Query Priority
Prefer: getByRole > getByLabelText > getByText > getByTestId.
screen.getByRole('heading', { name: /welcome/i });
screen.getByTestId('welcome-heading');
Async with findBy and waitFor
await screen.findByRole('alert', { name: /success/i });
screen.getByRole('alert', { name: /success/i });
Avoid Implementation Details
await user.click(screen.getByRole('button', { name: /add to cart/i }));
expect(screen.getByText(/1 item in cart/i)).toBeInTheDocument();
expect(wrapper.state('cartCount')).toBe(1);
Asserting Absence
Use queryBy* (never getBy*) for negative DOM assertions — getBy* throws if absent, making .not assertions unreliable.
expect(screen.queryByRole('alert')).not.toBeInTheDocument();
expect(screen.queryByText(/error/i)).toBeNull();
await user.click(screen.getByRole('button', { name: /close/i }));
expect(screen.queryByRole('dialog')).not.toBeInTheDocument();
expect(screen.getByRole('alert')).not.toBeInTheDocument();
See unit-testing skill for the broader strategy of testing both presence and absence.
Decision Tree
Element present now?
→ getByRole / getByText
Appears after async?
→ findByRole / findByText
Should NOT exist?
→ queryByRole (returns null)
User input?
→ userEvent.setup() then user.type(), user.click()
No accessible query?
→ Add aria-label; getByTestId last resort
Custom hook?
→ renderHook(() => useMyHook())
Side effects?
→ waitFor(() => expect(...))
Example
import { render, screen } from '@testing-library/react';
import userEvent from '@testing-library/user-event';
import { ContactForm } from './ContactForm';
describe('ContactForm', () => {
it('should submit and show success', async () => {
const onSubmit = jest.fn().mockResolvedValue({ ok: true });
const user = userEvent.setup();
render(<ContactForm onSubmit={onSubmit} />);
await user.type(screen.getByRole('textbox', { name: /name/i }), 'Ada');
await user.type(screen.getByRole('textbox', { name: /email/i }), 'ada@test.com');
await user.click(screen.getByRole('button', { name: /send/i }));
expect(onSubmit).toHaveBeenCalledWith({ : , : });
( screen.()).();
});
});
Edge Cases
- Portals/modals: Use
screen queries since portals render outside parent DOM
- Async state: Wrap assertions in
waitFor when state updates after await or setTimeout
- Act warnings: Ensure async operations complete;
findBy* handles automatically
- Providers: Create
renderWithProviders wrapper for context (theme, router, store)
- Cleanup: RTL calls
cleanup automatically with Jest; do not call manually
Checklist
Resources