Snapshot Testing
Snapshot tests capture formatted output (JSON, HTML, error messages) and compare future runs against committed snapshots. insta provides review workflow and inline snapshots.
Search across all documentation pages
Snapshot tests capture formatted output (JSON, HTML, error messages) and compare future runs against committed snapshots. insta provides review workflow and inline snapshots.
[dev-dependencies]
insta = { version = "1", features = ["json"] }#[test]
fn renders_user_json() {
let json = serde_json::to_string_pretty(&user()).unwrap();
insta::assert_snapshot!(json);
}When to reach for this: Stable formatted output, CLI help text, serializers, and error display strings.
use insta::assert_json_snapshot;
#[test]
fn order_response_shape() {
let resp = OrderResponse {
id: 1,
total_cents: 500,
status: "pending".into(),
};
assert_json_snapshot!(resp);
}cargo test
# on intentional change:
INSTA_UPDATE=1 cargo test order_responseWhat this demonstrates:
snapshots/*.snap filesINSTA_UPDATE=1 accepts new golden outputassert_json_snapshot! redacts unstable fields with settingsinsta::with_settings!({
filters => vec![
(r"\d{4}-\d{2}-\d{2}".to_string(), "[DATE]".to_string()),
],
}, {
assert_snapshot!(format_log_line());
});#[insta::snapshot] macro for inline snapshots in sourcetests/snapshots/
└── my_test__renders_user_json.snap
| Alternative | Use When | Don't Use When |
|---|---|---|
assert_eq! on structs | Small stable values | Large formatted strings |
| Property tests | Algebraic laws | Visual CLI layout |
| Golden binary files | Image/audio output | Text JSON APIs |
insta gives diff UI, redaction, inline snapshots, and review workflow.
insta::assert_snapshot!(val, @"expected"); stores expected in source after INSTA_UPDATE=1.
No - they are regression gates. Update intentionally when output changes.
Await in test, snapshot formatted string or JSON body.
Use distinct snapshot names: assert_snapshot!("case_a", out_a).
assert_snapshot!(cmd_help_string()) after clap CommandFactory::command().render_help().
Regex filter in with_settings! replacing UUID pattern with [UUID].
Default under crate root snapshots/; configure with insta settings in Cargo.toml metadata if needed.
Re-run INSTA_UPDATE=1 locally after resolving test code conflicts.
Possible but rare; props usually use assertions, not snapshots, due to volume.
Stack versions: This page was written for Rust 1.97.0 (edition 2024), Tokio 1.x, Axum 0.8, serde 1.0, sqlx 0.8, clap 4, and Polars 0.46+.
Reviewed by Chris St. John·Last updated Jul 16, 2026