React Testing Library Best Practices

Write effective tests using React Testing Library with proper queries and assertions.

6/11/2026
Prompt
You are a React Testing Library expert. Help me write better component tests.

## Component to Test
Component code: ${{COMPONENT_CODE}}
Component purpose: ${{PURPOSE}}
User interactions: ${{INTERACTIONS}}
Edge cases: ${{EDGE_CASES}}

## Please Provide:

1. **Query Selection**
```typescript
// Priority order:
// - getByRole
// - getByLabelText
// - getByPlaceholderText
// - getByText
// - getByTestId
```

2. **User Event Testing**
```typescript
// userEvent over fireEvent:
// - Click interactions
// - Typing
// - Selection
// - Keyboard events
```

3. **Async Testing**
```typescript
// Async patterns:
// - waitFor
// - findBy queries
// - act() usage
// - Timeout handling
```

4. **Custom Render**
```typescript
// Test utilities:
// - Provider wrapper
// - Router wrapper
// - Custom options
```

5. **Mocking**
```typescript
// Mock strategies:
// - MSW for APIs
// - Module mocks
// - Context mocking
// - Timer mocking
```

6. **Accessibility Testing**
```typescript
// A11y assertions:
// - jest-axe
// - Role verification
// - Label associations
```

7. **Test Organization**
```typescript
// Structure:
// - describe blocks
// - Test naming
// - Setup/teardown
// - Shared fixtures
```

8. **Complete Test Suite**
```typescript
// Full tests for your component
```

Or press ⌘C to copy

Variables (4)

Replace these placeholders with your own content before using the prompt.

[{COMPONENT_CODE][{PURPOSE][{INTERACTIONS][{EDGE_CASES]

Ready to use?

Copy and paste into your favorite AI tool

Works With

Test this prompt live

Try it in our AI Playground with Claude

Open Playground

Explore more Coding prompts

Browse Coding

Command Palette

Search for a command to run...