React Testing Library patterns for component testing
View on GitHubplugins/aai-stack-react/skills/react-testing/SKILL.md
February 1, 2026
Select agents to install to:
npx add-skill https://github.com/the-answerai/alphaagent-team/blob/main/plugins/aai-stack-react/skills/react-testing/SKILL.md -a claude-code --skill react-testingInstallation paths:
.claude/skills/react-testing/# React Testing Skill
Patterns for testing React components with React Testing Library.
## Testing Philosophy
React Testing Library encourages testing components the way users interact with them, not implementation details.
### The Guiding Principles
1. Test behavior, not implementation
2. Find elements like a user would
3. Interact like a user would
4. Assert on what the user sees
## Rendering Components
### Basic Rendering
```tsx
import { render, screen } from '@testing-library/react'
test('renders greeting', () => {
render(<Greeting name="World" />)
expect(screen.getByText('Hello, World!')).toBeInTheDocument()
})
```
### Rendering with Providers
```tsx
function renderWithProviders(
ui: ReactElement,
{
preloadedState = {},
...renderOptions
} = {}
) {
function Wrapper({ children }: { children: ReactNode }) {
return (
<Provider store={setupStore(preloadedState)}>
<ThemeProvider>
<Router>
{children}
</Router>
</ThemeProvider>
</Provider>
)
}
return render(ui, { wrapper: Wrapper, ...renderOptions })
}
// Usage
test('renders with providers', () => {
renderWithProviders(<UserDashboard />)
expect(screen.getByText('Dashboard')).toBeInTheDocument()
})
```
## Queries
### Query Priority
Use queries in this order (most to least preferred):
```tsx
// 1. Accessible queries (preferred)
screen.getByRole('button', { name: /submit/i })
screen.getByLabelText('Email')
screen.getByPlaceholderText('Enter email')
screen.getByText('Hello World')
screen.getByDisplayValue('current value')
// 2. Semantic queries
screen.getByAltText('Profile picture')
screen.getByTitle('Close')
// 3. Test IDs (last resort)
screen.getByTestId('custom-element')
```
### Query Variants
```tsx
// getBy* - Throws if not found (use for elements that should exist)
screen.getByRole('button')
// queryBy* - Returns null if not found (use for asserting absence)
expect(screen.queryByText('Loading')).not.