Learn the fundamentals of testing in React. We’ll cover unit testing, writing assertions with Jest, and ensuring your app's reliability.
Previously in this course, we explored how to enhance our application's robustness using Introduction to PropTypes: Catching Bugs with React Type Checking. While type checking helps catch interface errors, it doesn't verify that your business logic actually works. In this lesson, we introduce formal testing to ensure your code behaves exactly as expected.
At its core, testing is the practice of automating the verification of your code. Instead of manually clicking through your movie browser every time you change a line of code, you write "test" files that describe how a specific part of your application should behave.
A unit test focuses on the smallest possible unit of code—usually a single function or a single component. By isolating these units, you can verify that they produce the correct output for a given input. This approach to quality assurance is what prevents "regressions," where fixing one bug accidentally introduces another.
In the JavaScript ecosystem, Jest is the industry-standard framework for testing. It provides a "test runner" (which finds and executes your tests) and an "assertion library" (which allows you to check if results match your expectations).
To use Jest in your Vite project, you typically install it along with a testing library for React. While we won't cover the full configuration here, know that your test files will usually end in .test.js or .spec.js.
An assertion is simply a statement that says, "I expect X to be Y." If X is not Y, the test fails.
Let's look at a simple utility function in our movie browser. Imagine we have a function that formats a movie's release date.
JAVASCRIPT// utils/formatDate.js export const formatDate = (year) => CE9178">`Released in ${year}`; // utils/formatDate.test.js import { formatDate } from CE9178">'./formatDate'; test(CE9178">'formats the year correctly', () => { const result = formatDate(2023); expect(result).toBe(CE9178">'Released in 2023'); });
Here, test() defines the test case, and expect().toBe() is our assertion. If the function logic changes and returns something else, the test runner will alert you immediately.
Testing components is similar, but we need to "render" them into a virtual environment. We use render from the testing library to create a snapshot of the component in memory.
JAVASCRIPT// components/MovieTitle.test.js import { render, screen } from CE9178">'@testing-library/react'; import MovieTitle from CE9178">'./MovieTitle'; test(CE9178">'renders the movie title correctly', () => { render(<MovieTitle title="Inception" />); const titleElement = screen.getByText(/Inception/i); expect(titleElement).toBeInTheDocument(); });
This test checks that our MovieTitle component actually displays the text it receives via props. If you accidentally break the component later, this test will fail, providing you with instant feedback.
In your movie browser project, locate the logic that filters your movie array. Create a new test file and perform the following:
filterMovies.test.js file.npm test in your terminal to see the results.Testing is your safety net. By using Jest and the React Testing Library, you shift from "hoping" your code works to "knowing" it works. We’ve covered:
expect().toBe()) to verify outcomes.This foundation prepares you for Finalizing the Movie Browser: A Professional Guide to QA and Testing, where we will apply these techniques to the full project.
Up next: Debugging React Apps — How to use DevTools to inspect state and performance bottlenecks.
Learn how to initialize a professional React project using Vite. We’ll cover the project directory structure and how to launch your fast development environment.
Read moreStop overwhelming your server with every keystroke. Learn how to implement debouncing in React to optimize API performance and create snappier UIs.
Introduction to Testing