insta Snapshot Testing for Rust
repository·master·Indexed 23 days ago
https://github.com/mitsuhiko/instaA snapshot testing library for Rust that allows developers to assert complex values against reference snapshots. It includes the cargo-insta CLI tool for reviewing, accepting, and rejecting snapshot changes, as well as a VSCode extension for syntax highlighting and integrated diffing of .snap and RON files.
What's inside insta
- insta is a snapshot testing library for Rust. Snapshot tests (also known as approval tests) assert values against a reference value (the snapshot). Unlike standard string assertions, snapshot tests are designed for complex values and provide tools to review changes, making them ideal for large or frequently changing reference values.
Use the VS Code extension for insta snapshots
masterThevscode-instaextension improves the workflow for Rust'sinstasnapshot testing by providing syntax highlighting, a dedicated view for pending snapshots, and integrated diffing tools. It supports.snapfiles (with YAML-like highlighting) and RON snapshots.Use cargo-insta commands
masterThe
cargo-instaCLI provides several commands to manage and review snapshots generated by theinstalibrary:test: Runs your tests.review: Starts an interactive process to review all identified snapshot changes. This is the primary way to inspect and decide on snapshot updates.accept: Directly accepts all identified snapshot changes without an interactive review.reject: Directly rejects all identified snapshot changes without an interactive review.
For a complete list of flags and advanced usage, refer to the official CLI documentation.
Jump to snapshot assertion definition
masterOnce the extension is loaded, you can navigate from a snapshot assertion macro in your Rust code directly to its corresponding snapshot file by pressingF12on the macro.Manage pending snapshots via the Sidebar
masterIfinstais used in your project, all pending snapshots are displayed in a dedicated view in the VS Code sidebar. Clicking on a snapshot in this view opens a diff view where you can accept or reject the changes. This functionality also works for inline snapshots.Install cargo-insta
masterYou can install
cargo-instausing prebuilt binaries via shell scripts, manual binary downloads, or by compiling from source usingcargo install.Using Installation Scripts
Unix:
curl -LsSf https://insta.rs/install.sh | shWindows:
powershell -c "irm https://insta.rs/install.ps1 | iex"Installing a Specific Version
To install a specific version (e.g., 1.38.0), use the versioned installer scripts:
Unix:
curl -LsSf https://github.com/mitsuhiko/insta/releases/download/1.38.0/cargo-insta-installer.sh | shWindows:
powershell -c "irm https://github.com/mitsuhiko/insta/releases/download/1.38.0/cargo-insta-installer.ps1 | iex"Installing via Cargo
You can manually build and install the tool using
cargo install. When installing an older version, it is recommended to use the--lockedflag to honor theCargo.lockfile.$ cargo install cargo-insta --version 1.15.0 --lockedInstall the VSCode extension for insta
masterYou can enhance your workflow by installing themitsuhiko.instaVSCode extension. This extension provides syntax highlighting for.snapfiles and allows you to review snapshots directly within the editor.Use inline snapshots with cargo-insta
masterFor testing where snapshots are stored directly within your source files instead of in separate.snapfiles, use the companioncargo-instatool.Optimize insta performance in dev builds
masterInsta performs better when compiled in release mode. You can optimize the
instaandsimilarcrates specifically in yourCargo.tomlto reduce memory usage and speed up diffing without slowing down your entire dev build.[profile.dev.package.insta] opt-level = 3 [profile.dev.package.similar] opt-level = 3Interactive Review Mode shortcuts
masterWhen running
cargo insta review, you can interactively manage snapshots using the following keyboard shortcuts:aorEnter: accept (keep the new snapshot)A: accept all remaining snapshotsrorEscape: reject (retain the old snapshot)R: reject all remaining snapshotssorSpace: skip (keep both for now)S: skip all remaining snapshotsi: Toggle extended snapshot infod: Toggle snapshot diffo: open snapshot files in an external tool (available for binary snapshots)
Configure test behavior with Settings
masterThe
insta::Settingsstruct allows you to customize how snapshot testing behaves on a per-thread basis. You can modify settings like map sorting, snapshot paths, and redactions. Because settings are thread-local, you should useclone_current()to modify existing settings or usebindmethods to apply changes temporarily to a specific scope or future.use insta; let mut settings = insta::Settings::clone_current(); settings.set_sort_maps(true); settings.bind(|| { // runs the assertion with the changed settings enabled insta::assert_snapshot!(...); });Accept or Reject snapshots
masterYou can bulk accept or reject snapshots using the
acceptandrejectcommands.cargo insta accept: Accepts all pending snapshots.cargo insta reject: Rejects all pending snapshots.cargo insta accept --snapshot '<key>': Accepts a specific snapshot.cargo insta reject --snapshot '<key>': Rejects a specific snapshot.