RE

React Testing Library

Used in 995 javascript projects (avg ★260)

JavaScript
Testing
Used by 995 projects

Details

Language / Topic
javascriptJavaScript
Category
Testing

Rules

balanced
- For JavaScript: Structure tests with describe('ComponentName') and it('descriptive scenario') blocks.
- Render components via render(<ComponentName prop="value" />) and query elements using screen.getByRole('button') or screen.getByText('text').
- Simulate interactions with const user = userEvent.setup(); await user.click(screen.getByRole('button')).
- Assert presence or state with expect(screen.getByText('expected')).toBeInTheDocument().
- Create mock handler functions and verify calls with expect(mockFn).toHaveBeenCalledWith(expectedArgs).
- For JavaScript: Clear all mocks in beforeEach to ensure test isolation and consistent starting state.
- Handle async behavior with waitFor(() => expect(element).toBeInTheDocument()) for updates after interactions.
- Prefer accessible queries like getByRole over getByTestId to mimic real user behavior and improve test resilience.
- Render with required props or context to match real usage and avoid default prop issues.
- Combine multiple assertions in a single test for related user flows.