This Claude skill specializes in generating efficient, high-quality unit tests for React components using Jest and React Testing Library. It emphasizes a minimalist approach—prioritizing one comprehensive, robust test over multiple brittle ones—to ensure codebase stability. It strictly adheres to Jest-specific syntax, provides templates for mocking API calls, and enforces best practices for handling asynchronous rendering and DOM queries.
Use Cases
Automating the creation of unit tests for React components to verify correct rendering and prop handling using Jest.
Mocking complex API responses and global fetch calls to test dynamic data display without external dependencies.
Refactoring brittle or redundant test suites into a more maintainable 'minimalist' structure that reduces CI/CD failure points.
Implementing proper asynchronous testing patterns using act() and waitFor() to resolve 'not wrapped in act' warnings in React.
Ensuring accessible and resilient DOM queries by implementing role-based and case-insensitive text searching strategies.
name
unit-test
description
Unit tests with Jest + React Testing Library. CRITICAL - Uses Jest (NOT Vitest).
Unit Test (Jest + Testing Library)
⚠️ JEST ONLY - NOT Vitest
// Jest globals (no import needed)
jest.fn(), jest.mock(), jest.clearAllMocks()
// Vitest (will fail!)
import { vi } from 'vitest' // ERROR!
⭐⭐⭐ MINIMALIST: 1-2 TESTS PER COMPONENT ⭐⭐⭐
Rule: ONE comprehensive test > MANY brittle tests
Component Type
Max Tests
Focus
Static (no fetch)
1
Renders with props
Async (with fetch)
2
Heading + loaded data
DO NOT TEST (will cause failures):
Empty state
Error state
Loading skeletons
Each element separately
Elements that may not exist
Combine multiple assertions in ONE test:
it('renders correctly', async () => {
// Multiple expects in ONE test = good
expect(heading).toBeInTheDocument();
expect(item1).toBeInTheDocument();
expect(item2).toBeInTheDocument();
});
2. MULTIPLE ELEMENTS: Use getAllByText() or getByRole()
// Text appears multiple times
const elements = screen.getAllByText(/science/i);
expect(elements.length).toBeGreaterThan(0);
// Be specific with role
expect(screen.getByRole('heading', { name: /science/i })).toBeInTheDocument();
// Read source code first
expect(screen.getByRole('heading')).toBeInTheDocument();
// Don't assume elements exist
expect(screen.getByPlaceholderText(/search/i)).toBeInTheDocument(); // May not exist!