Convert Figma logo to code with AI

mitsuhiko logoinsta

A snapshot testing library for rust

2,127
95
2,127
63

Top Related Projects

A bunch of lints to catch common mistakes and improve your Rust code. Book: https://doc.rust-lang.org/clippy/

5,953

Format Rust code

A Rust compiler front-end for IDEs

12,551

The Rust package manager

Quick Overview

Insta is a snapshot testing library for Rust. It simplifies the process of writing and maintaining tests by allowing developers to compare the output of their code against stored snapshots, automatically updating them when changes are intentional.

Pros

  • Easy to use and integrate into existing Rust projects
  • Supports multiple snapshot formats (including inline, file-based, and YAML)
  • Automatically updates snapshots when running tests with the INSTA_UPDATE environment variable
  • Provides a CLI tool for managing snapshots

Cons

  • Requires careful review of snapshot changes to ensure they are intentional
  • Can lead to brittle tests if not used judiciously
  • May increase test suite size due to stored snapshots
  • Limited to Rust projects only

Code Examples

  1. Basic snapshot test:
use insta::assert_snapshot;

#[test]
fn test_hello_world() {
    let result = "Hello, World!";
    assert_snapshot!(result);
}
  1. Snapshot with dynamic content:
use insta::assert_debug_snapshot;

#[test]
fn test_user_info() {
    let user = User {
        name: "Alice",
        age: 30,
        created_at: chrono::Utc::now(),
    };
    assert_debug_snapshot!(user, {
        ".created_at" => "[dynamic]",
    });
}
  1. Inline snapshot:
use insta::assert_yaml_snapshot;

#[test]
fn test_config() {
    let config = Config {
        api_key: "secret",
        max_retries: 3,
        timeout: Duration::from_secs(30),
    };
    assert_yaml_snapshot!(config, @r###"
    ---
    api_key: secret
    max_retries: 3
    timeout: 30s
    "###);
}

Getting Started

  1. Add insta to your Cargo.toml:

    [dev-dependencies]
    insta = "1.28.0"
    
  2. Write a test using insta:

    #[cfg(test)]
    mod tests {
        use insta::assert_snapshot;
    
        #[test]
        fn test_example() {
            let result = format!("Example output");
            assert_snapshot!(result);
        }
    }
    
  3. Run the test:

    INSTA_UPDATE=1 cargo test
    
  4. Review and commit the generated snapshot files.

Competitor Comparisons

A bunch of lints to catch common mistakes and improve your Rust code. Book: https://doc.rust-lang.org/clippy/

Pros of rust-clippy

  • Comprehensive linting tool for Rust, offering a wide range of checks and suggestions
  • Integrated with the Rust ecosystem and regularly updated with new lints
  • Helps enforce best practices and catch common mistakes in Rust code

Cons of rust-clippy

  • Can be overwhelming with the number of lints and configuration options
  • May produce false positives or overly pedantic warnings in some cases
  • Requires manual integration into projects and CI pipelines

Code comparison

rust-clippy:

#[warn(clippy::all)]
fn example() {
    let x = 5;
    if x == 5 { println!("x is five"); }
}

insta:

#[test]
fn test_example() {
    let result = format!("x is {}", 5);
    insta::assert_snapshot!(result);
}

Summary

rust-clippy is a powerful linting tool for Rust, offering extensive checks and suggestions to improve code quality. It's well-integrated with the Rust ecosystem but can be complex to configure and may produce occasional false positives.

insta, on the other hand, is a snapshot testing library for Rust, focusing on simplifying the process of writing and maintaining tests. It provides an easy way to create and update snapshots, making it particularly useful for testing complex data structures or output.

While both tools aim to improve code quality, they serve different purposes: rust-clippy for static analysis and linting, and insta for snapshot testing and verification.

5,953

Format Rust code

Pros of rustfmt

  • Official Rust formatting tool, ensuring widespread adoption and community support
  • Integrates seamlessly with the Rust ecosystem and toolchain
  • Highly configurable with numerous options for customizing code style

Cons of rustfmt

  • Focused solely on code formatting, lacking snapshot testing capabilities
  • May require additional setup and configuration for specific project needs
  • Limited to Rust language, while Insta supports multiple languages

Code Comparison

rustfmt example:

fn main() {
    println!("Hello, world!");
}

Insta example:

#[test]
fn test_snapshot() {
    let value = compute_value();
    insta::assert_snapshot!(value);
}

While rustfmt focuses on formatting Rust code according to style guidelines, Insta is primarily used for snapshot testing. The code examples demonstrate their different purposes: rustfmt ensures consistent code formatting, while Insta allows for easy creation and verification of test snapshots.

Both tools serve different purposes in the Rust ecosystem. rustfmt is essential for maintaining consistent code style across projects, while Insta simplifies the process of writing and maintaining snapshot tests. Developers may find both tools valuable in their Rust projects, depending on their specific needs for code formatting and testing.

A Rust compiler front-end for IDEs

Pros of rust-analyzer

  • Comprehensive Rust IDE support with advanced features like code completion, go-to-definition, and refactoring
  • Actively maintained by the Rust community with frequent updates and improvements
  • Integrates well with various editors and IDEs, providing a consistent development experience

Cons of rust-analyzer

  • Larger project scope and complexity, which may lead to a steeper learning curve for contributors
  • Higher resource consumption due to its extensive analysis capabilities
  • May require more setup and configuration compared to simpler tools

Code comparison

rust-analyzer:

fn main() {
    let mut world = World::new();
    world.create_entity().with(Position { x: 0.0, y: 0.0 }).build();
    world.maintain();
}

insta:

#[test]
fn test_snapshot() {
    let value = compute_value();
    insta::assert_debug_snapshot!(value);
}

Summary

rust-analyzer is a powerful Rust IDE backend with extensive features, while insta is a snapshot testing library for Rust. rust-analyzer offers comprehensive IDE support but may be more complex to set up and use. insta provides a simpler, focused solution for snapshot testing but lacks the broader development tooling of rust-analyzer.

12,551

The Rust package manager

Pros of Cargo

  • Comprehensive package management and build system for Rust
  • Extensive ecosystem integration with crates.io
  • Robust dependency resolution and version management

Cons of Cargo

  • Steeper learning curve for beginners
  • More complex configuration for advanced use cases
  • Larger codebase and slower compilation times

Code Comparison

Cargo (Cargo.toml):

[package]
name = "my_project"
version = "0.1.0"
authors = ["Your Name <you@example.com>"]
edition = "2018"

[dependencies]
serde = "1.0"
tokio = { version = "1", features = ["full"] }

Insta (src/lib.rs):

use insta::assert_snapshot;

#[test]
fn test_snapshot() {
    let value = calculate_something();
    assert_snapshot!(value);
}

Cargo is a comprehensive build system and package manager for Rust, offering robust dependency management and integration with the Rust ecosystem. It excels in handling complex projects but may have a steeper learning curve.

Insta, on the other hand, is a snapshot testing library for Rust, focusing on simplifying the process of writing and maintaining snapshot tests. It offers a more straightforward API for specific testing scenarios but lacks the broad project management features of Cargo.

Convert Figma logo designs to code with AI

Visual Copilot

Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.

Try Visual Copilot

README

insta: a snapshot testing library for Rust

Build Status Crates.io License rustc 1.51.0 Documentation VSCode Extension

Introduction

Snapshots tests (also sometimes called approval tests) are tests that assert values against a reference value (the snapshot). This is similar to how assert_eq! lets you compare a value against a reference value but unlike simple string assertions, snapshot tests let you test against complex values and come with comprehensive tools to review changes.

Snapshot tests are particularly useful if your reference values are very large or change often.

Example

#[test]
fn test_hello_world() {
    insta::assert_debug_snapshot!(vec![1, 2, 3]);
}

Curious? There is a screencast that shows the entire workflow: watch the insta introduction screencast. Or if you're not into videos, read the 5 minute introduction.

Insta also supports inline snapshots which are stored right in your source file instead of separate files. This is accomplished by the companion cargo-insta tool.

Editor Support

For looking at .snap files there is a vscode extension which can syntax highlight snapshot files, review snapshots and more. It can be installed from the marketplace: view on marketplace.

jump to definition

Diffing

Insta uses similar for all its diffing operations. You can use it independently of insta. You can use the similar-asserts crate to get inline diffs for the standard assert_eq! macro to achieve insta like diffs for regular comparisons:

use similar_asserts::assert_eq;

fn main() {
    let reference = vec![1, 2, 3, 4];
    assert_eq!(reference, (0..4).collect::<Vec<_>>());
}

Sponsor

If you like the project and find it useful you can become a sponsor.

License and Links