--- name: snapshot-testing description: Snapshot testing with insta (Rust) — automatic output capture, review workflow, inline snapshots. Use when testing complex output (JSON, AST, HTML) where writing manual assertions is impractical. license: MIT --- ## What I do Guide snapshot testing—capture output automatically rather than manually writing assertions. ## Problem with Traditional Assertions ```rust // Tedious for complex outputs assert_eq!(config.timeout, 30); assert_eq!(config.max_retries, 3); assert_eq!(config.endpoint, "https://api.example.com"); ``` Issues: tedious to write, easy to make mistakes, doesn't show actual output. ## Snapshot Testing ```rust use insta::assert_snapshot; #[test] fn test_parsed_config() { let config = parse_config(); assert_snapshot!(config); } ``` First run: passes, creates snapshot file. Subsequent runs: compares to saved snapshot. ## Update Snapshots ```bash cargo insta test --accept # Interactive cargo insta accept # Accept all ``` ## insta (Rust) ```toml [dev-dependencies] insta = "1.40" ``` ### Basic Usage ```rust use insta::assert_snapshot; #[test] fn test_serialize() { let value = MyStruct { name: "test".into(), count: 42 }; assert_snapshot!(value); } ``` ### Inline Snapshots ```rust use insta::assert_inline_snapshot; #[test] fn test_format() { let result = format_number(42.0); assert_inline_snapshot!(result, @"42.00"); } ``` ## When to Use ### Good Candidates - Serialized output (JSON, YAML, TOML) - Formatted strings - Error messages - Configuration - Complex data structures - ASCII waveforms/diagrams ### Bad Candidates - Simple primitives (`assert_eq!(x, 5)` is clearer) - Random values (timestamps, UUIDs) - Implementation details ## Best Practices 1. **Review snapshots** before accepting 2. **Use descriptive names**: `assert_snapshot!("user_login_success", response)` 3. **Commit snapshots**: They're part of the test suite 4. **Update deliberately**: Use `cargo insta`, don't manually edit --- Use this skill when testing complex output that would require many assertions.