---
title: "Snapshot Testing For the Masses"
description: "A minimal snapshot testing library can be built without macros, external tools or editor integration: the core primitive is a self-updating string literal that records its source file and line (via Zig's @src()) and rewrites itself in place on mismatch."
url: "https://swe.quest/articles/snapshot-testing-for-the-masses-1d23ec44f416430c"
canonical: "https://tigerbeetle.com/blog/2024-05-14-snapshot-testing-for-the-masses"
source: "TigerBeetle"
published: "2024-05-14"
difficulty: "medium"
kind: "How-to"
reading_minutes: 8
tags: ["testing","zig","developer-tools"]
---

# Snapshot Testing For the Masses

A post on [TigerBeetle](https://swe.quest/sources/tigerbeetle.md), published 2024-05-14, 8 min read, How-to. Original: [https://tigerbeetle.com/blog/2024-05-14-snapshot-testing-for-the-masses](https://tigerbeetle.com/blog/2024-05-14-snapshot-testing-for-the-masses)

## Summary

A minimal snapshot testing library can be built without macros, external tools or editor integration: the core primitive is a self-updating string literal that records its source file and line (via Zig's @src()) and rewrites itself in place on mismatch. Because the snapshot is a first-class value rather than an assertion macro, it composes into helpers like diff\_fmt, diff\_json and domain-specific check functions, as in TigerBeetle's CLI argument parser tests. Practical details covered include opt-in updates via an env var, splicing in correctly indented string literals, tracking line shifts across multiple updates, touching the filesystem only when a test would fail anyway, CRLF pitfalls, and an \<snap:ignore\> marker for volatile fields like timestamps. The whole thing fits in under 500 lines.

Written by swe.quest, not by the author.

**Difficulty:** Medium: assumes a working developer; shows how it is built or done

## Topics

- [Testing](https://swe.quest/tags/testing.md) (Engineering practices)
- [Zig](https://swe.quest/tags/zig.md) (Languages)
- [Developer tools](https://swe.quest/tags/developer-tools.md) (Engineering practices)

## Prerequisites

- unit testing basics ([easy posts on Testing](https://swe.quest/tags/testing.md?difficulty=easy))
- Zig syntax ([easy posts on Zig](https://swe.quest/tags/zig.md?difficulty=easy))

## Keywords

TigerBeetle, Zig, snapshot testing, insta

## Related posts

- [Bun v0.5.8](https://swe.quest/articles/bun-v0-5-8-2656173dc4d056ee.md): Bun · 2023-03-18 · Medium · 8 min read · JavaScript, Runtimes & VMs, Testing
  Bun v0.5.8 adds Jest-compatible snapshot testing to bun test via expect().toMatchSnapshot(), with --update-snapshots and a --rerun-each flag for reproducing flaky tests.
- [Bun v1.1.7](https://swe.quest/articles/bun-v1-1-7-443b16215b4eb538.md): Bun · 2024-05-03 · Medium · 7 min read · JavaScript, Runtimes & VMs, Debugging
  Bun v1.1.7 fixes 28 bugs and adds glob patterns in package.json workspaces (via Bun.Glob), asymmetric matcher support in expect.extend's this.equals, and a working bunx --version.
- [How to Write Code Without Having to Read It](https://swe.quest/articles/how-to-write-code-without-having-to-read-it-1700fbc56278687d.md): Shopify Engineering · 2022-10-11 · Medium · 12 min read · Type systems, Testing, Refactoring & migrations, TypeScript, Developer productivity
  Engineers can safely change unfamiliar code with minimal reading by letting the compiler, linter and unit tests guide them: locate the entrypoint via user-visible strings (or git bisect for regressions), make the breaking change at the leaf component, then climb the callstack following build errors and sweep back down via lint errors, exhaustive switches and test failures.
- [What if writing tests was a joyful experience?](https://swe.quest/articles/what-if-writing-tests-was-a-joyful-experience-24be6abe32cc317e.md): Jane Street Tech Blog · 2023-01-09 · Medium · 11 min read · Testing, Functional programming, Developer productivity
  Expect tests (snapshot tests) let you write a blank \`\[%expect\]\` block under code that prints output; the test runner fills in the actual output as a diff you can accept in your editor, making test-writing feel like a REPL session.
- [Bun v1.0.14](https://swe.quest/articles/bun-v1-0-14-1b4cfbfd459b3e35.md): Bun · 2023-11-22 · Medium · 4 min read · JavaScript, Runtimes & VMs, Developer tools
  Bun v1.0.14 adds Bun.Glob, a Zig-implemented glob API with scan() for async file iteration and match() for string matching, claimed to match strings 3x faster than fast-glob or micromatch.

Browse: [Latest posts](https://swe.quest/index.md) · [Topics](https://swe.quest/tags.md) · [Sources](https://swe.quest/sources.md) · [Topic map](https://swe.quest/map.md) · [Site guide (llms.txt)](https://swe.quest/llms.txt)
