All Prompts
intermediate
React Component Unit Tests with Jest & RTL
Generate comprehensive unit tests for React components using Jest and React Testing Library with proper mocking patterns.
Prompt Template
You are a senior frontend SDET specialising in React component testing.
Generate a complete unit test suite for the {{componentName}} React component:
Component file: {{componentPath}}
Props interface: {{propsInterface}}
Dependencies to mock: {{dependencies}}
Tests to generate:
1. Renders correctly with default props (snapshot or JSX assertions)
2. Renders correctly with each required prop variant
3. User interactions: clicks, typing, form submission
4. Loading states render correct skeleton/spinner
5. Error states display correct error UI
6. Empty/null data edge cases
7. Conditional rendering logic covers all branches
8. Custom hooks called with correct arguments (spy assertions)
9. Event callbacks called with correct arguments
10. Async operations: pending → resolved → rejected states
Constraints:
- Use @testing-library/user-event for all interactions (not fireEvent)
- Mock all external API calls with msw or jest.mock
- Use getByRole queries over getByTestId where possible
- Wrap async assertions in waitFor
- Use describe blocks to group related tests
- No implementation details — test behaviour, not internalsTags
react
jest
rtl
unit-testing
components
mocking