Skip to main content
QA Training
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 internals
Tags
react
jest
rtl
unit-testing
components
mocking

How to use this prompt

  1. 1Copy the prompt template using the Copy button above.
  2. 2Open your preferred AI assistant (ChatGPT, Claude, Gemini, Copilot, etc.).
  3. 3Paste the prompt and replace the {{placeholder}} variables with your project-specific values.
  4. 4Review the output - treat it as a professional first draft, not a final deliverable.
  5. 5Iterate: refine the output by asking follow-up questions or providing more context.

Frequently asked questions

Which AI models work best with this prompt?v

This prompt works with any capable language model including ChatGPT (GPT-4o), Claude (3.5+), Gemini (1.5 Pro+), and GitHub Copilot Chat. More capable models produce more complete and accurate outputs.

How do I adapt the prompt for a different framework?v

Replace the framework placeholder with your target framework and update any framework-specific syntax references in the output. The structure and logic of the generated tests will transfer across frameworks.

Can I use this prompt commercially?v

Yes. All prompts on QATraining.uk are free to use for personal and commercial QA work. Attribution is appreciated but not required.