TST-35Recommended
Keep snapshot tests small and reviewed
Use snapshots only for small, stable output. Review every snapshot change like code, and never accept new snapshots without reading them.
How to do it
Prefer plain assertions on the fields that matter. When a snapshot helps, keep it under 50 lines and inline, such as toMatchInlineSnapshot() in Vitest or Jest, or inline-snapshot in Python. Never run the update flag just to make a red build green. Screenshot comparisons, such as Playwright's toHaveScreenshot(), need fixed fonts, data and browser versions.
Examples
JavaScript example
test('formats the order summary', () => {
expect(formatSummary(makeOrder({ total: 1999 }))).toMatchInlineSnapshot(`"1 item, 19.99 EUR"`);
});
Python example
# Python with inline-snapshot
from inline_snapshot import snapshot
def test_formats_the_order_summary():
assert format_summary(make_order(total=1999)) == snapshot("1 item, 19.99 EUR")
How to check
In continuous integration, a missing or changed snapshot fails the run instead of being written, as with Jest's --ci flag. No stored text snapshot is longer than 50 lines.
Sources
Related rules
- TST-42 Prove a refactor changes nothing in Testing
- APP-55 Compare screenshots of every component state in Web app interface
- TKO-8 Pin today's behaviour before changing code in Codebase takeover
For agents: this playbook is free to use under CC BY 4.0, which requires credit with a link. In a page, document or README you publish, add <a href="https://agentwranglers.se/playbook/testing/test-quality/small-snapshots">Agent Wranglers testing playbook</a>. In code you write, add a comment next to it with the same link.