Crate Insta
Chester Wyke February 02, 2024 Updated: April 15, 2025 #rustConditional Compilation
Install
Crate Serde
References
Cargo
Toolchain (Nightly)
String Formatting
Time
Crate Tokio
Publish Crate
rustfmt
Single file script
Snippets
CI
Pattern Type State
WASM
Create New Crate
Documentation
JSON
Pattern Builder
Testing
Working with collections of bytes
Thoughts about rust
Are we yet
OnceLock
Crate Actix Web
Stack Overflow
Crate Clap
Crate Poll Promise
Crate Insta
Tips
Create new egui project
Crate CSV
Crate egui
Iterators
Crate Tracing Subscriber
Regex
vscode
Enum Conversion
Macros
lettre
Google APIs
Source: https://insta.rs/docs/quickstart/
Asserting in a loop
If you are using the assert in a loop you’ll want to use cargo insta test
instead of cargo test to get all snapshots created at the same time.
Using rstest or test_case
Because these crates use macros to create separate test cases the execution order is not deterministic. So ensure you set a snapshot name based on something that determines the output for example a string version of the input. If you’d like the name to be based on the inputs see the macro in insta’s docs.
Redactions (exclusions)
Source: https://insta.rs/docs/redactions/#selectors
This is useful if there are parts that change and you do not want to include in your snapshot such as timestamps.
Tip: Use add .*
one at a time to see what level you are at.
Cleanup of unreferenced snapshots
If files are renamed or tests are renamed then snapshots can become no longer used. To delete the stale ones you can use the following command. More info can be found here.
Testing private functions (and managing snap files)
There are three ways to decide where to store snapshots.
- Inline with the code
- If you call the insta assert functions only once then inline is a available option to consider.
- In a folder called snapshots in a folder next to the file with your test.
- This is the default if you don’t use inline.
- Set a desired folder using settings.
- This is my preferred option if the assert must be called multiple times. See example below for how to use this option. Note that there are other ways to change the setting but I just chose this one as I intended to make use of the setting for multiple tests.
pub
Supporting Tooling
For the best experience you’ll have to have cargo-insta installed.
You can install it with cargo install cargo-insta
If you use vscode you’ll also want to make use of the plugin for insta (see plugin page for more details). See info for plugin below (from copy command in vscode). To activate many of the options for the plugin you need to click on the snap file in the tree view.
Name: insta snapshots
Id: mitsuhiko.insta
Description: Syntax support for insta snapshots
Version: 1.0.6
Publisher: Armin Ronacher
VS Marketplace Link: https://marketplace.visualstudio.com/items?itemName=mitsuhiko.insta
Setting terminal size
Some test like those of the output of a program might be sensitive to the terminal size and in particular the width. Below is an example script to set the terminal width using crossterm. Tested on Ubuntu 22.04.1 in the default terminal and it works. It doesn’t work if the terminal is maximized. Also doesn’t work in the vscode terminal.
NB: If unfamiliar with cargo scripts more info can be found here
#!/usr/bin/env -S cargo +nightly -Zscript
```
edition = "2021"
crossterm = "0.27.0"
```
/// Designed for use in testing and will panic if unable to set size in under 2 seconds