Skip to content

In development. Expect rough edges and changes. The source code is not public yet.

TigerBeetle 8 min read How-to mediumAdept

Snapshot Testing For the Masses

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.

Related posts

  • Bun v0.5.8

    Bun mediumAdept

    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. A new --preload flag (or bunfig.toml preload list) runs setup files such as Bun.plugin() loaders before scripts and tests, and bun install workspaces now accept a trailing /* glob. Web API fixes bring Request/Response null-body handling, blob.slice content types and Request constructor validation in line with Chrome and Safari. Node.js compatibility work covers Buffer, os.cpus on macOS, os.networkInterfaces and AbortSignal in http, and Bun.sleepSync now takes milliseconds, a breaking change.

  • Bun v1.1.7

    Bun mediumAdept

    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. Several fixes come with root causes: a SIMD invalid memory access in Zig's std.mem.indexOfScalarPos crashing Windows startup, a misassumed 8-byte alignment of FILE_DIRECTORY_INFORMATION, threadlocal destructors running during std::quick_exit with live Workers, an uninitialized C++ integer corrupting stacktrace line numbers, and a memory leak from unread Bun.serve() request bodies since v1.0.30. Smaller changes include WebSocket close code 1015 on TLS failures, a ServerWebSocket shrink from 32 to 24 bytes, and adding clang-analyzer to CI.

  • How to Write Code Without Having to Read It

    Shopify Engineering mediumAdept

    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. To make code easier for the next "non-reader," replace comments with enforced checks, wrap primitives in custom types, merge dependent arguments into tagged unions, nested objects or enums, and split decision logic into pure functions so unit tests stay simple. Examples use React Native and TypeScript but the approach generalizes to any typed, tested codebase.

  • What if writing tests was a joyful experience?

    Jane Street Tech Blog mediumAdept

    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. Jane Street uses them for most of its OCaml test suite, from rounding-function tables and Bonsai web UI DOM diffs to magic-trace symbol demangling and order-book state machines. Because they capture whole printed state rather than hand-picked assertions, they implicitly guard against regressions you didn't anticipate. Good expect tests depend on helper functions and pretty-printers that keep the output concise and readable.

  • Bun v1.0.14

    Bun mediumAdept

    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. It fixes a bun install race condition where non-unique temporary extraction paths let concurrent extractions of different versions of the same package overwrite each other. Inside node_modules Bun now prefers .js over .ts files to avoid untranspilable TypeScript sources. ESM/CommonJS mixing errors are now caught at build time with clearer messages, and the release also improves build and import error output, console.log class names, and process.kill support for SIGKILL/SIGSTOP.

The quest for software engineering knowledge.

Look:
Layout: