DEV Community

Luca Barbato
Luca Barbato

Posted on

1

Quick notes on using insta

Insta is a nice snapshot testing tool for Rust.

Snapshot testing

Snapshot tests are a more general extension of the normal tests we have in Rust.

Usually we use assert_eq! and similar macros. Insta let you deal with the reference values in a richer way and store them inside or outside your codebase.

Pitfalls to keep in mind

I spent way too much time stumbling upon a bunch of annoyances while converting some tests in rust-code-analysis so it is better to list them so you will avoid to suffer from similar mistakes:

  • cargo insta is git-aware, make sure to pass --no-ignore if your snapshots directory is populated by different means even better if you can use git-submodules.
  • If you deal with floating points, round them to the precision you care about. Different arches/libc may have different precisions
  • If you save paths keep in mind that Windows is special in this regard and you may have to manually serialize it.

Nice things to keep in mind

  • cargo test -- --nocapture provides enough information to see what went wrong.
  • cargo insta test --review makes adding more tests and make sure you do not break anything by mistake.

That's all for today

Hostinger image

Get n8n VPS hosting 3x cheaper than a cloud solution

Get fast, easy, secure n8n VPS hosting from $4.99/mo at Hostinger. Automate any workflow using a pre-installed n8n application and no-code customization.

Start now

Top comments (0)

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay