What is snapshot testing?

Answer

Snapshot testing captures the output of a component/function at a point in time and saves it as a reference snapshot file. On subsequent test runs, the output is compared to the saved snapshot — if it differs, the test fails. Popularized by Jest for React component testing: expect(component.render()).toMatchSnapshot();. The first run creates a .snap file. On changes: if intentional, update snapshots (jest --updateSnapshot); if unintentional, fix the bug. Benefits: easy to write, catches unintended UI changes, good for large complex outputs. Pitfalls: (1) Brittle: any UI change (even intentional) requires snapshot updates. (2) Meaningless diffs: large snapshot diffs are hard to review. (3) False positives: devs blindly update snapshots without verifying the change is correct. (4) Implementation coupling: snapshots capture implementation details. Best used for: stable, deterministic outputs (serializers, formatters, API responses). Avoid for frequently changing UI components — use explicit behavioral assertions instead.