In this tutorial, we will cover how to write unit and integration tests for React components. Ensuring your code works properly before deployment is critical, and automated testing provides a reliable way to verify your components behave as expected.
You will learn how to:
Prerequisites:
Before starting, you should have a basic understanding of:
Unit tests focus on a single unit of code, such as a function or component, and ensure it behaves as expected in isolation.
Jest
Jest is a JavaScript testing framework developed by Facebook. It's widely used for its simplicity and features like zero-configuration, built-in code coverage reports, and out-of-the-box support for Babel, TypeScript, and React.
React Testing Library
React Testing Library (RTL) encourages writing maintainable tests. It provides light utility functions on top of react-dom and react-dom/test-utils, making it easier to test React components.
While unit tests focus on isolated parts, integration tests verify that multiple parts work together correctly. In a React application, this could mean testing that several components render and function together as expected.
Unit Testing with Jest and RTL
// Import libraries
import { render, screen } from '@testing-library/react';
import userEvent from '@testing-library/user-event';
import Button from '../Button';
// Test suite
describe('Button', () => {
// Test case
test('renders with text', () => {
render(<Button>Click me!</Button>);
expect(screen.getByText('Click me!')).toBeInTheDocument();
});
// Test case
test('handles onClick', () => {
const handleClick = jest.fn();
render(<Button onClick={handleClick}>Click me!</Button>);
userEvent.click(screen.getByText('Click me!'));
expect(handleClick).toHaveBeenCalledTimes(1);
});
});
Integration Testing with Jest and RTL
// Import libraries
import { render, screen } from '@testing-library/react';
import userEvent from '@testing-library/user-event';
import Form from '../Form';
// Test suite
describe('Form', () => {
// Test case
test('submitting form', () => {
render(<Form />);
userEvent.type(screen.getByLabelText(/name/i), 'John Doe');
userEvent.click(screen.getByRole('button', { name: /submit/i }));
expect(screen.getByText(/Thanks for submitting, John Doe!/i)).toBeInTheDocument();
});
});
In this tutorial, we covered how to write unit and integration tests for React components using Jest and React Testing Library. Remember, unit tests focus on isolated parts, while integration tests ensure that multiple parts function together correctly.
Exercise 1: Write a unit test to check if a Counter
component correctly increments and decrements a value.
Exercise 2: Write an integration test for a simple TodoApp
that includes AddTodo
and TodoList
components. Test if when a user adds a todo, it appears in the list.
Exercise 3: Write an integration test for a LoginForm
component. Test if when a user inputs their username and password and submits the form, they receive a welcome message.
Remember, practice is key in mastering testing. Keep writing tests for your components and you'll soon see the benefits in your productivity and code quality.