Skip to main content
Snapshot testing saves the output of a value and compares it against future test runs. This is particularly useful for UI components, complex objects, or any output that needs to remain consistent.

Basic Snapshots

Snapshot tests are written using the .toMatchSnapshot() matcher:
test.ts
The first time this test is run, the argument to expect will be serialized and written to a special snapshot file in a __snapshots__ directory alongside the test file.

Snapshot Files

After running the test above, Bun will create:
directory structure
The snapshot file contains:
__snapshots__/snap.test.ts.snap
On future runs, the argument is compared against the snapshot on disk.

Updating Snapshots

Snapshots can be re-generated with the following command:
terminal
This is useful when:
  • You’ve intentionally changed the output
  • You’re adding new snapshot tests
  • The expected output has legitimately changed

Inline Snapshots

For smaller values, you can use inline snapshots with .toMatchInlineSnapshot(). These snapshots are stored directly in your test file:
test.ts
After the first run, Bun automatically updates your test file:
test.ts

Using Inline Snapshots

  1. Write your test with .toMatchInlineSnapshot()
  2. Run the test once
  3. Bun automatically updates your test file with the snapshot
  4. On subsequent runs, the value will be compared against the inline snapshot
Inline snapshots are particularly useful for small, simple values where it’s helpful to see the expected output right in the test file.

Error Snapshots

You can also snapshot error messages using .toThrowErrorMatchingSnapshot() and .toThrowErrorMatchingInlineSnapshot():
test.ts
After running, the inline version becomes:
test.ts

Advanced Snapshot Usage

Complex Objects

Snapshots work well with complex nested objects:
test.ts

Array Snapshots

Arrays are also well-suited for snapshot testing:
test.ts

Function Output Snapshots

Snapshot the output of functions:
test.ts

React Component Snapshots

Snapshots are particularly useful for React components:
test.ts

Property Matchers

For values that change between test runs (like timestamps or IDs), use property matchers:
test.ts
The snapshot will store:
snapshot file

Custom Serializers

You can customize how objects are serialized in snapshots:
test.ts

Best Practices

Keep Snapshots Small

test.ts

Use Descriptive Test Names

test.ts
test.ts

Handle Dynamic Data

test.ts

Managing Snapshots

Reviewing Snapshot Changes

When snapshots change, carefully review them:
terminal

Cleaning Up Unused Snapshots

Bun will warn about unused snapshots:
warning
Remove unused snapshots by deleting them from the snapshot files or by running tests with cleanup flags if available.

Organizing Large Snapshot Files

For large projects, consider organizing tests to keep snapshot files manageable:
directory structure

Troubleshooting

Snapshot Failures

When snapshots fail, you’ll see a diff:
diff
Common causes:
  • Intentional changes (update with --update-snapshots)
  • Unintentional changes (fix the code)
  • Dynamic data (use property matchers)
  • Environment differences (normalize the data)

Platform Differences

Be aware of platform-specific differences:
test.ts