Snapshot testing serialize output (component render, JSON object, string) và save vào .snap file — subsequent runs compare với saved snapshot, fail nếu thay đổi.
- Khi thay đổi là intentional: jest --updateSnapshot (hoặc -u) update tất cả snapshots; jest --updateSnapshot --testNamePattern để update specific.
- Inline snapshots: toMatchInlineSnapshot() lưu snapshot trong test file thay vì separate .snap file — dễ review hơn trong PRs nhưng clutters test files.
- Khi nên dùng: stable UI components ít thay đổi (Button, Card, Input), serializable data structures, API response shapes.
- Khi không nên dùng: frequently changing components (mỗi PR update snapshot không meaningful), dynamic content (timestamps, random IDs — phải mock), complex business logic (unit tests với explicit assertions tốt hơn).
Pitfall phổ biến: commit outdated snapshots khi developers chạy update mà không review changes — snapshot tests trở thành rubber stamp.