Open In App

Snapshot Testing in React

Last Updated : 21 Feb, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

Snapshot testing is a powerful testing technique in React that helps ensure UI consistency by capturing a snapshot of the rendered component and comparing it with future snapshots. It is commonly used with Jest, a popular JavaScript testing framework.

What is Snapshot Testing?

Snapshot testing is the testing technique used in React that captures the rendered output of a component and compares it to a previously saved "snapshot" of that output.

  • Render the component: For rendering the react component in the controlled testing environment, the Jest testing library is used.
  • Capture the Snapshot: The testing library takes a "snapshot" of the rendered component. This snapshot is usually a JSON representation of the component's DOM structure
  • Store the snapshot: The snapshot is saved in a special folder called __snapshots__, which is created alongside your test files.
  • Compare on subsequent runs: Each time we run our tests, Jest re-renders the component and compares the new output to the saved snapshot. If everything matches, the test passes. If there are any differences, the test fails, alerting you to changes in the UI.

Setting Up Snapshot Testing in React

1. Create a React Project

npx create-react-app react-app
cd snapshot-testing-demo

2. Install Dependencies

Install Jest and React Test Renderer:

npm install --save-dev @testing-library/react @testing-library/jest-dom jest

3. Create a Simple Component

Create a React component that we will test.

JavaScript
//Button.js

import React from "react";

const Button = ({ label }) => {
    return <button>{label}</button>;
};

export default Button;

4. Write a Snapshot Test

Create a test file for the component inside the __tests__ directory.

JavaScript
import React from "react";
import { render } from "@testing-library/react";
import Button from "../components/Button";

test("Button component renders correctly", () => {
    const { asFragment } = render(<Button label="Click me" />);
    expect(asFragment()).toMatchSnapshot(); 
});

Folder Structure

Output

5. Run the Snapshot Test

npm test

Jest creates a snapshot file inside the __snapshots__ folder, if the test passes.

exports[`Button component renders correctly 1`] = `
<button>
Click me
</button>
`;

6. Handling Snapshot Updates

If the component changes (e.g., new props or UI updates), the test will fail.

npm test -- -u

Output

Output
snapshot testing in React.

In this example

  • The Button component is a simple React component that takes a label prop and renders a <button> element with the label text passed to it.
  • The test is written using React Testing Library and Jest. It imports the render function from @testing-library/react and the Button component.
  • The test renders the Button component with a label ("Click me") using render(<Button label="Click me" />).
  • The asFragment() method is used to capture the rendered output as a snapshot, and expect(asFragment()).toMatchSnapshot() checks if the rendered output matches the saved snapshot. If it changes, the test will fail, helping detect unintended changes.

Use Cases for Snapshot Testing

  • Component Rendering Validation: Ensure that UI components render consistently after changes or updates.
  • Cross-Browser Testing: By capturing snapshots from various environments they verify that components render correctly across different browsers.
  • Third-Party Library Updates: Make sure that when you update external libraries, they don't accidentally change how your app looks.

Limitations of Snapshot Testing

  • False Positives: Tests may pass even if the UI has minor, unintended changes that aren't easily noticeable.
  • Overhead in Large Snapshots: Large snapshots can be difficult to manage and review.
  • Not a Substitute for Functional Testing: Snapshot tests verify the structure, not the functionality, of components.

Next Article

Similar Reads