React Testing Library And Jest- The Complete Guide May 2026
// Use userEvent instead of fireEvent await user.click(button)
test('toggles state on click', async () => const user = userEvent.setup() render(<Toggle />)
test('should increment counter', () => const result = renderHook(() => useCounter(0)) React Testing Library and Jest- The Complete Guide
// Test behavior, not implementation expect(screen.getByText('Welcome John')).toBeInTheDocument()
await user.click(button) expect(button).toHaveTextContent('OFF') ) test('shows error for invalid email', async () => const user = userEvent.setup() render(<SignupForm />) await user.type(screen.getByLabelText(/email/i), 'invalid') await user.click(screen.getByRole('button', name: /submit/i )) // Use userEvent instead of fireEvent await user
// Async (for elements that appear later) await screen.findByText('Loaded')
export default testEnvironment: 'jsdom', setupFilesAfterEnv: ['<rootDir>/src/setupTests.js'], transform: ts, async () =>
expect(await screen.findByText('Valid email required')).toBeInTheDocument() ) ✅ DO // Query by accessible name screen.getByRole('button', name: /submit/i ) // Use findBy for async elements expect(await screen.findByText('Loaded')).toBeInTheDocument()