When Snapshots Are Updated
You'll learn: How to intentionally update snapshot files when your component output legitimately changes.
The Update Decision
Snapshot tests fail when actual output doesn't match the saved snapshot. But not every failure means broken code—sometimes you've intentionally changed your component's structure, styling, or text. When this happens, you need to tell your test runner "yes, this new output is correct, update the snapshot file."
Think of it like updating your employee ID photo after a haircut. The security system flags you because you look different—not because something's wrong, but because your appearance legitimately changed. You need to officially update your photo on file.
The --updateSnapshot Flag
Most test runners provide a flag to accept changes:
# Jest/Vitest
npm test -- --updateSnapshot
# or shorthand
npm test -- -u
# Node test runner
node --test --update-snapshots
When you run this command, the test runner:
- Executes your tests
- Compares actual output to snapshots
- Overwrites snapshot files with new output
- Marks tests as passing
When to Update
Update snapshots when:
- You've intentionally refactored JSX/HTML structure
- You've changed component text or labels
- You've updated styling output
- You've added or removed elements purposefully
Never blindly update snapshots without reviewing the diff. Always examine what changed first—if the change surprises you, you've caught a bug, not a legitimate update.
The Workflow
- Make your intentional code changes
- Run tests normally (they fail)
- Review the snapshot diff carefully
- If changes look correct, run with
--updateSnapshot - Commit the updated
.snapfiles alongside your code
Key Takeaway: Use --updateSnapshot to accept intentional changes after carefully reviewing diffs—it's your way of saying "yes, this new output is the correct baseline going forward."