Back to Blog
Lesson 45 of the React Fundamentals: Build Modern UIs from Scratch course
ReactJune 25, 20264 min read

Introduction to Testing: Quality Assurance in React with Jest

Learn the fundamentals of testing in React. We’ll cover unit testing, writing assertions with Jest, and ensuring your app's reliability.

ReactTestingJestSoftware QualityWeb DevelopmentJavaScriptfrontend

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.

Understanding Unit Testing

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.

Getting Started with Jest

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.

Writing Your First Assertion

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 a React Component

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.

Hands-on Exercise: Testing the Filter Logic

In your movie browser project, locate the logic that filters your movie array. Create a new test file and perform the following:

  1. Create a filterMovies.test.js file.
  2. Write a test that imports your filtering function.
  3. Assert that when a specific search term is passed, the function returns only the matching movies.
  4. Run npm test in your terminal to see the results.

Common Pitfalls

  • Testing Implementation Details: Beginners often try to test the internal state of a component rather than what the user actually sees. Always aim to test the output (what is rendered) rather than the private variables inside the component.
  • Over-mocking: You don't need to mock every single dependency. If your utility functions are pure (no side effects), just import and test them directly.
  • Ignoring Failures: If a test fails, don't just delete it. It’s telling you that your assumptions about your code are wrong or that you've broken something important.

Recap

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:

  • Unit testing as a way to isolate logic.
  • Assertions (expect().toBe()) to verify outcomes.
  • Running tests to ensure your movie browser remains stable.

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.

Similar Posts