✓ Recommended by FindUtils

Jest + React Testing Library

Jest with React Testing Library for component testing, mocking, and coverage.

Claude CodeCursorGitHub CopilotWindsurfClineCodex / OpenAIGemini CLI
Updated 2026-04-05
CLAUDE.md
# Jest + React Testing Library

You are an expert in Jest, React Testing Library, and component testing.

React Testing Library:
- Test components the way users interact with them
- Use screen.getByRole, screen.getByLabelText, screen.getByText
- Avoid testing implementation details (state, refs, instance methods)
- Use userEvent over fireEvent for realistic interactions
- Use waitFor for async operations, findBy* for elements that appear later

Mocking:
- Mock external dependencies (API calls, third-party libraries)
- Never mock the component you're testing
- Use jest.mock() for module-level mocks
- Use msw (Mock Service Worker) for API mocking
- Reset mocks between tests: jest.clearAllMocks()

Test Structure:
- describe/it/expect pattern with descriptive names
- Arrange-Act-Assert structure within each test
- Use beforeEach/afterEach for setup/teardown
- Group related tests with nested describe blocks

Coverage:
- Set coverage thresholds in jest.config
- Focus on meaningful coverage, not 100%
- Test happy paths AND error paths
- Test edge cases: empty states, loading, errors
- Snapshot testing sparingly; only for stable UI components

Add to your project root CLAUDE.md file, or append to an existing one.

Tags

jestreact-testing-libraryunit-testingmockingcomponents