React Unit Testing: A Complete Guide with Jest & Testing Library - Part 3: Testing Components with Jest
João
- •
- 10 MIN TO READ
React Unit Testing: A Complete Guide with Jest & Testing Library - Part 1: Unit Tests and Their Importance
📖 5 min read | 💻 Intermediate | ⚡ With Code Examples
If you're building React applications for production, understanding unit testing isn't just a nice-to-have—it's essential. Let's dive deep into why and how.
Unit tests are the foundation of your testing strategy. They focus on testing individual components or functions in isolation to ensure each piece of your application works as expected.
1// Example of a unit being tested
2const sum = (a, b) => a + b;
3
4// The unit test
5test('sum adds two numbers correctly', () => {
6 expect(sum(1, 2)).toBe(3);
7});
8
The testing pyramid consists of three main layers:
React Testing Library promotes:
1// ❌ Don't test implementation details
2test('state updates correctly', () => {
3 const { result } = renderHook(() => useState(false));
4 expect(result.current[0]).toBe(false);
5});
6
7// ✅ Do test user behavior
8test('button toggles feature', () => {
9 render(<FeatureToggle />);
10 const button = screen.getByRole('button');
11 fireEvent.click(button);
12 expect(screen.getByText('Feature Enabled')).toBeInTheDocument();
13});
14
1// ❌ Bad: Testing state directly
2expect(component.state('isOpen')).toBe(true);
3
4// ✅ Good: Testing what the user sees
5expect(screen.getByText('Menu')).toBeVisible();
6
1// ❌ Bad: Large, brittle snapshots
2expect(component).toMatchSnapshot();
3
4// ✅ Good: Specific behavioral tests
5expect(screen.getByRole('button')).toHaveTextContent('Submit');
6
1// ❌ Bad: Testing React's behavior
2expect(component).toBeInstanceOf(React.Component);
3
4// ✅ Good: Testing your component's behavior
5expect(screen.getByRole('alert')).toHaveTextContent('Error occurred');
6
Follow these principles:
1// Arrange: Set up your test
2render(<Counter initialCount={0} />);
3
4// Act: Perform the action
5fireEvent.click(screen.getByText('+'));
6
7// Assert: Check the result
8expect(screen.getByText('1')).toBeInTheDocument();
9
🚀 Continue your learning journey with React Unit Testing: Part 2 - Configuring Your React Project for Unit Testing In Part 2, we dive into setting up your testing environment in React with Jest and Testing Library. Learn how to create custom configurations, recommended file structures, and tips for seamless CI/CD integration. Don’t miss out!