Snapshot Testing in React
Last Updated :
21 Feb, 2025
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

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
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.
Similar Reads
NextJS Jest Testing Jest is a delightful JavaScript testing framework with a focus on simplicity. It's widely used with React applications and integrates well with Next.js to provide robust testing capabilities. This article explores how to set up and use Jest for testing in a Next.js application.Steps to Create an App
2 min read
Recipe Finder using ReactJS In this project article, we will be creating a recipe finder application using the React library. We have given the application the name "GeeksforGeeks Recipe Finder". This application is completely developed in ReactJS and mainly focuses on the functional components and also manages the various sta
5 min read
Introduction to Testing in MERN Testing in MERN (MongoDB, ExpressJS, React, NodeJS) applications is important for ensuring the reliability, functionality, and stability of your software. Testing helps identify bugs early in the development process, maintain code quality, and build confidence in your application's behavior. In this
4 min read
Storybook Using React Storybook is an open-source tool to build the UI components individually, allowing developers to concentrate on one component at a time, examine it more deeply, and record its behavior and changes. The objective of the project is to increase the effectiveness of the development of React components b
4 min read
Typing Speed Tester using React In this article, we will create a Typing Speed Tester that provides a random paragraph for the user to type as accurately and quickly as possible within a fixed time limit of one minute. This application also displays the time remaining, counts mistakes calculates the words per minute and characters
9 min read
ReactJS Static Type Checking In React, Static Type Checking becomes more and more complex. Therefore, it becomes important to use static type checking. It is mainly preferred to prevent bugs and errors. Since react library is built on top of JavaScript. It is dynamic, weakly typed, and loosely typed. Static Type Checking is use
2 min read