Skip to main content
Snapshot testing captures the output of your code and saves it to a file. Future test runs compare against the saved snapshot, making it easy to catch unexpected changes.

Basic usage

Use toMatchSnapshot() to create a snapshot:
The first time this test runs, Bun creates a snapshot file:
The snapshot file contains:

Inline snapshots

Inline snapshots write the expected value directly into your test file:
After the first run, Bun updates your test file:

Named snapshots

Provide a name to identify the snapshot:

Updating snapshots

When your output changes intentionally, update snapshots:
Or use the shorthand:
This updates all snapshots to match the current output.

Property matchers

Use property matchers to ignore dynamic values:
The snapshot will be:

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:
To update snapshots in CI:

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:
Press u to update the snapshot or s to skip.