Lesson 59 of 80
A snapshot test catches that something changed, not whether that change was a real regression — and that gap has a well-known failure mode.
12 min read
Every option explains itself — why the right one is right, and why each wrong one is wrong. Miss one and it comes back later on a spaced-repetition schedule.
This topic is about judgment and practical tradeoffs more than a single right answer — a mock interview is the better way to test yourself on it.
Be interviewed on this →Hints without spoilers, and a plain explanation of why a wrong answer was wrong — answered only from this lesson. Free account, no card.
Sign up freeWhat interviewers actually ask about this, and what a strong answer has to cover.
A strong answer covers
Likely follow-ups
A snapshot test renders a component, serializes the output (usually the rendered markup), and saves it to a file. On every later run, the test re-renders the component and diffs the new output against the saved file. Any difference — intentional or not — fails the test.
That's the whole mechanism, and it's genuinely useful for one specific job: catching unintended changes to output you didn't mean to touch. It's a poor substitute for testing behavior — whether a button actually does the right thing when clicked — which is what the testing lesson's interaction-based approach is for. Snapshot testing checks shape; behavioral testing checks what happens.
import { render } from "@testing-library/react";
function UserCard({ name, role }) {
return (
<div className="card">
<h2>{name}</h2>
<p>{role}</p>
</div>
);
}
test("renders a user card", () => {
const { asFragment } = render(<UserCard name="Adesh" role="Frontend Engineer" />);
expect(asFragment()).toMatchSnapshot();
});
The first run writes a .snap file:
exports[`renders a user card 1`] = `
<div class="card">
<h2>Adesh</h2>
<p>Frontend Engineer</p>
</div>
`;
Now someone renames the class from card to user-card as part of an unrelated CSS cleanup. The test fails — correctly, something did change. But here's the failure mode: the fastest way to make a red test suite green is jest --updateSnapshot, which overwrites the saved file with whatever the component renders right now, no matter whether that's correct. Run that command without reading the diff, and you've just approved every accidental change alongside the intentional one — the test suite is green and tells you nothing.
--updateSnapshot is a batch accept-all, not a review tool. It regenerates every failing snapshot in one pass with no per-change confirmation — the diff you should have read is exactly the thing this command lets you skip.The testing lesson covers rendering a component and asserting on behavior — clicking, typing, checking what the user would actually see happen. Snapshot testing answers a narrower, mechanical question on top of that: "did the output change at all since last time," which is worth asking for some things and worth skipping for most interactive behavior, where a behavioral assertion says what you actually mean far more precisely.