Testing in React 19
Testing ensures your React 19 application is reliable, maintainable, and bug-free. Use a combination of unit, integration, and end-to-end tests.
Unit Testing
- Use Jest, Vitest, or React Testing Library for unit tests.
- Test components, hooks, and context in isolation.
Example: Component Unit Test (React Testing Library)
import { render, screen, fireEvent } from '@testing-library/react';
import Counter from './Counter';
test('increments value on click', () => {
render(<Counter />);
fireEvent.click(screen.getByText('Increment'));
expect(screen.getByText('1')).toBeInTheDocument();
});
Integration Testing
- Test interactions between components and services.
- Mock API calls and external dependencies.
End-to-End (E2E) Testing
- Use Cypress or Playwright for E2E tests.
- Simulate real user flows (navigation, form submission, etc.).
Best Practices
- Write tests for critical logic and user flows.
- Run tests in CI pipelines for every commit/PR.
- Use coverage tools to monitor test completeness.