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!