How It Works
- First run — hitspec saves the response body to a snapshot file.
- Subsequent runs — hitspec compares the current response against the saved snapshot.
- Mismatch — if the response has changed, the test fails with a diff.
- Update — use
--update-snapshotsto accept the new response as the baseline.
Basic Usage
Add thesnapshot assertion operator in your expect block:
"getUserResponse" is the snapshot name. It must be unique within the test file.
Snapshot Files
Snapshots are stored in a__snapshots__/ directory next to your test file. Each
test file gets a single <filename>.snap.json file that holds all of that file’s
named snapshots:
tests/api.http stores its snapshots in tests/__snapshots__/api.snap.json,
keyed by the snapshot names you give in expect body snapshot "...". Snapshots
group by test file, not one file per snapshot.
Updating Snapshots
When an API response changes intentionally (new fields, updated values), update the snapshots:Workflow
A typical snapshot testing workflow:- Write the test with
expect body snapshot "name" - Run once to generate the initial snapshot
- Commit the snapshot file alongside your test
- CI runs compare responses against the committed snapshots
- When the API changes, run
--update-snapshotslocally, review the diff, and commit
Combining with Other Assertions
Snapshot testing works alongside other assertion operators:Snapshots compare the full response body. If your API returns dynamic values (timestamps, UUIDs, etc.), those will cause snapshot mismatches on every run. Use field-level assertions for dynamic values and reserve snapshots for stable response shapes.