Snapshot Test
Question
What does a snapshot test do?
Hint
Snapshots are useful for stable, complex output.
Explanation
A snapshot test captures the output of a function/component and saves it to a file. On subsequent runs, the output is compared against the saved snapshot. If they differ, the test fails (with a diff). To update: jest --updateSnapshot. Use cases: 1) React component rendering (serialize to JSON). 2) API response shape. 3) Error messages. 4) Config objects. Benefits: catch unintended changes with one assertion. Risks: 1) Snapshots can become stale (blindly updating passes bad changes). 2) Hard to understand what changed. 3) Not suitable for dynamic/time-based output. Review snapshot diffs carefully during code review.
Related Resources
Related Lessons
- Snapshot Testing
Learn snapshots.
Look up unfamiliar terms
A beginner-friendly glossary explains jargon in plain English — variables, functions, DOM, Promise, and more.
Decode error messages
Plain-English explanations of common errors — what they mean, why they happen, and how to fix them.
Build real projects
Apply what you learned by building guided projects with starter code and solutions.