
Snapshot testing is immensely popular for testing React apps or other component-based UIs. However, itâs not exactly drama-freeâââmany people looooove snapshots for their ease of use and ability to quickly bootstrap a testing portfolio, while others feel that the long-term effects of snapshots might be more harmful than they are helpful.
At the end of the day, snapshot testing is simply another tool in our tool belt. And while many people may be divided on how and when to use snapshot testing, itâs good to know that it exists and that itâs available when we need it.
Iâll be honest about my position on snapshotsâââI tend to be in the camp thatâs less enthusiastic about them. However, I recently came across a situation with some legacy code where it felt like snapshot tests were a perfect match. Using snapshots as a refactoring tool helped me successfully tackle and refactor some tricky code written long before I joined my company.
If youâre not familiar with snapshot tests, weâll do a little refresher. In a snapshot test, a âpictureâ of your codeâs output is taken the first time the test runs. This âpictureâ gets saved to a text file in your codebase and all subsequent test runs use this picture as a referenceâââif your codeâs output produces an identical snapshot, the test passes. However, if the output is different from the saved snapshot the test fails.
Hereâs an example of what a snapshot test looks like in Jest:
import renderer from "react-test-renderer";
function Test({ message }) {
return {message};
}
test("renders", () => {
const wrapper = renderer.create(<Test message="test" />);
expect(wrapper.toJSON()).toMatchSnapshot();
});
After this test runs for the first time it will create a snapshot file that future test runs will use as a reference. The snapshot file would look something like this:
// Jest Snapshot v1, https://goo.gl/fbAQLP exports[`renders 1`] = ` <div> test </div> `;
I hadnât heard about snapshot testing until I started using JestâââIâm not sure if the Jest team invented snapshot testing but they certainly popularized it! At first glance, snapshots are super convenientâââinstead of writing your own assertions you can just generate tests to see if your code is broken. Why waste time when the computer can automate our problems away? Jest even makes it super easy to automatically fix your snapshots. This means that even if youâve got a failing test youâre a single keypress away from fixing all of your tests.
At first glance, snapshot testing sounds like a dream come trueâââall I have to do is write one snippet of code to generate snapshots and Iâll have these super-detailed tests âfor freeâ? Take my money already!
However, over the past few years that Iâve been working with snapshot testing, Iâve found that snapshots introduce a number of pain points that make them difficult to maintain. And Iâm not the only one! For example, this company decided to ditch snapshots and wrote about it. Or consider this tweet:
I ð’d snapshot testing yesterday. Someone DM’d (not @’d!) me a question so here’s my response to why. I’ll blog soon https://t.co/Bxo4mS2BWr pic.twitter.com/6dmaRpHG2N
â Justin Searls (@searls) October 15, 2017
Thatâs not to say snapshot testing is all bad! After all, every tool has trade-offs, and itâs worth acknowledging the weaknesses of a tool when weâre evaluating using it. Here are a few reasons why Iâm not the biggest fan of having snapshots in my testing suites.
Snapshots are often used to test component trees or large objects. However, since the snapshot takes a picture of every single detail in the component/object, even the slightest change (like fixing a typo in a CSS class) will fail the snapshot test. As a result, you end up with tests that break even when the code still works. These false negatives create a lot of noise and erode your confidence in your testing suite.
You might be thinking, âIsnât this a good thing?â After all, being a single keypress away from a passing test suite sounds like a dream come true. However, because the tests are so easy to create/update, what tends to happen is that developers care less about the snapshot tests.
In my experience, developers will often simply press the button to update the snapshots without looking to see what changed or if the code is broken. While it is possible to treat your snapshots with the same importance as your code (and recommended in the Jest docs), it requires a ton of diligence. More often, my experience has been seeing engineers blindly update the snapshots and move on with their day (Iâve done it myself many times in the past ðą).
Itâs easy to generate a ton of test coverage using snapshots. If your team has a coverage threshold that all code has to meet, snapshots make hitting your coverage numbers a breeze. However, test coverage alone is not a sufficient metric to use to evaluate the quality of your test suite. While test coverage is a valuable tool for seeing gaps in your testing suite, it doesnât tell you about things like whether your tests are brittle, whether your code stands up to edge cases, or whether the tests accurately test the business requirements.
While Iâm not a fan of having snapshots as âlong-term residentsâ of my testing suites, Iâve actually come across a few use cases where they truly shine. For example, refactoring legacy code.
Rarely do we start a job and get thrown into greenfield projectsâââwe get codebases that have existed for years. And when we do, those projects can quickly go from a blank slate to nightmare codebase if weâre not careful. At some point in your career, youâre going to have to work on âlegacy codeâ that you didnât write. And many times those codebases donât have any tests.
When you start adding features to this legacy code, and youâre faced with a dilemma. You might need to refactor the code to fit new business requirements, but you donât want to run the risk of breaking something. In order to refactor safely, you need some type of tests in place.
The thing is, taking a pause to write tests for legacy code can sometimes feel like a luxury you donât have. After all, youâve got deadlines to hit, and you finally figured out where you need to modify this legacy code. If you take too long of a break you might lose that context that youâve built up!
Snapshots can actually be super useful to us in this scenario. Hereâs a snapshot testing workflow Iâve found super helpful when working with legacy code.
Read through the legacy code and try to get a picture of all of the various inputs that it could possibly have. However, you donât need to figure out the outputs! For each input variant, create a snapshot test. This helps you figure out what outputs are actually produced by the code youâre working with.
Since youâve got this massive safety net of snapshot tests to fall back on, start refactoring. Remember that this method of refactoring with snapshots is only good if you do not change the output at all. So if youâre working with a React component and you change the rendered output your snapshots will fail. This isnât the end of the world, just make sure to check why the snapshots failed and if the change was actually intended.
Once youâre done refactoring, you can safely replace these snapshots without fear of forgetting how you wanted to refactor the legacy code. However, for the reasons discussed above, you might not want those snapshots to be long-term residents of your testing suite. Now that the code isnât changing, you can safely start refactoring your tests. To make your tests more resilient long-term, you might want to consider taking each snapshot test and replacing it with a more focused assertion. For example, we could replace the snapshot test from before with this test using react-testing-library and jest-dom.
import { render } from "react-testing-library";
import "jest-dom/extend-expect";
function Test({ message }) {
return {message};
}
test("renders", () => {
const { getByText } = render(<Test message="test" />);
expect(getByText("test")).toBeInTheDocument();
});
Granted, this isnât an incredibly complex testâââthe component doesnât have any logic to refactor! These more focused assertions will stand the test of time (pun intended ð) better as the component changes with future requirements.
Throughout my (short) career Iâve seen lots of code written without tests by people that have long left the company. Itâs no secret that tricky, dense, difficult-to-read code has a negative effect on team morale and that, over time, code should be lovingly refactored to fit new requirements.
However, mocking or complaining about tricky legacy code shouldnât be our default responseâââinstead, we should try to always leave the code in better shape than when we found it.
This is easier said than done, especially when weâre trying to meet a tight deadline or if weâre afraid to touch the code lest we break something. This method of using Jest snapshots has been incredibly useful for me and I hope that you will find it useful too!
Thanks for reading! If you enjoyed this post, make sure to follow me on TwitterâââI make sure to post links to any new articles as I write them. If youâve had some snapshot test success stories, donât hesitate to reach out!
Install LogRocket via npm or script tag. LogRocket.init() must be called client-side, not
server-side
$ npm i --save logrocket
// Code:
import LogRocket from 'logrocket';
LogRocket.init('app/id');
// Add to your HTML:
<script src="https://cdn.lr-ingest.com/LogRocket.min.js"></script>
<script>window.LogRocket && window.LogRocket.init('app/id');</script>

Explore Git-driven agentic workflows with PatchPup and Fullsend, covering automation, CI checks, pull requests, and best practices for scaling AI coding agents.

Building the same support assistant with React Spectrum, Vercel AI Elements, and CopilotKit reveals differences in setup and UI control. Compare the code and integration work to choose an approach for your React app.

Can React Resizable Panelsâ Workspace replace Dockview or FlexLayout? Compare bundle size, tab state, and accessibility.

Learn to build a Python-based PDF search app with Typesense, embeddings, and semantic retrieval.
Would you be interested in joining LogRocket's developer community?
Join LogRocketâs Content Advisory Board. Youâll help inform the type of content we create and get access to exclusive meetups, social accreditation, and swag.
Sign up now