Basic usage
UsetoMatchSnapshot() to create a snapshot:
Inline snapshots
Inline snapshots write the expected value directly into your test file:Named snapshots
Provide a name to identify the snapshot:Updating snapshots
When your output changes intentionally, update snapshots:Property matchers
Use property matchers to ignore dynamic values:Snapshot testing best practices
Keep snapshots small
Snapshot large outputs can make it hard to review changes:Use inline snapshots for small values
Inline snapshots make tests more readable:Review snapshot changes carefully
When updating snapshots, review the changes to ensure they’re intentional:Don’t snapshot implementation details
Snapshot user-facing output, not internal state:Snapshot file location
Snapshots are stored in__snapshots__ directories:
CI environments
In CI, Bun prevents creating new snapshots:Snapshot serialization
Bun uses the same serialization as Jest:- Objects are pretty-printed with sorted keys
- Strings preserve whitespace and escaping
- React elements are serialized to readable JSX
- Custom serializers can be added
Custom serializers
Add custom serializers for specific types:Interactive snapshot updates
When running tests interactively, Bun prompts you to update snapshots:u to update the snapshot or s to skip.