new·The score now tells you which way it movedA brain's exam only ever grows: its own material writes questions, and so does every question a real caller asked and did not get answered. The score is a percentage over that growing set, so a brain that learned more could post a smaller number — and this week three did. One of them answered two MORE questions than the week before and showed eighteen points less. Printed as a single percentage, that reads as decline to a reader and as punishment to anyone who contributes material.all news →
mozg.beta
Sign in

Bun · Test runner · all subjects

snapshots

18 notes, read out of this brain and free to use. Each one was extracted from a source and is re-checked against its exam.

Update snapshots with --update-snapshots flag

To regenerate snapshots in Bun, use the --update-snapshots flag when running tests. For example: bun test --update-snapshots

Snapshot file location and naming

Snapshot files are stored in a __snapshots__ directory that is created alongside the test file. The snapshot file is named with the test file name plus the .snap extension, for example snap.test.ts.snap for a test file named snap.test.ts.

Snapshot testing with toMatchSnapshot

Bun's test runner supports Jest-style snapshot testing using the toMatchSnapshot() matcher. On the first run, Bun writes a snapshot file to a __snapshots__ directory alongside the test file.

Basic snapshot test example

import { test, expect } from "bun:test"; test("snapshot", () => { expect({ foo: "bar" }).toMatchSnapshot(); });

Write a snapshot test

Use the toMatchSnapshot() matcher to create snapshot tests: `expect({ a: 1 }).toMatchSnapshot();`. Update snapshots with the `--update-snapshots` flag when running tests.

toThrowErrorMatchingSnapshot matcher

The `.toThrowErrorMatchingSnapshot()` matcher snapshots error messages thrown by a function. Use it with `.toThrowErrorMatchingInlineSnapshot()` for inline error snapshots.

Example: basic snapshot test

```ts import { test, expect } from "bun:test"; test("snap", () => { expect("foo").toMatchSnapshot(); }); ``` This shows the basic usage of `.toMatchSnapshot()` which captures "foo" to the snapshot file on first run.

Example: inline snapshot test

```ts import { test, expect } from "bun:test"; test("inline snapshot", () => { expect({ hello: "world" }).toMatchInlineSnapshot(); }); ``` After the first run, Bun automatically updates the test file to include the snapshot: ```ts test("inline snapshot", () => { expect({ hello: "world" }).toMatchInlineSnapshot(` { "hello": "world", } `); }); ```

Example: error snapshot test

```ts import { test, expect } from "bun:test"; test("error snapshot", () => { expect(() => { throw new Error("Something went wrong"); }).toThrowErrorMatchingSnapshot(); expect(() => { throw new Error("Another error"); }).toThrowErrorMatchingInlineSnapshot(); }); ``` After running, the inline version becomes: ```ts test("error snapshot", () => { expect(() => { throw new Error("Something went wrong"); }).toThrowErrorMatchingSnapshot(); expect(() => { throw new Error("Another error"); }).toThrowErrorMatchingInlineSnapshot(`"Another error"`); }); ```

Example: snapshot with property matchers

```ts import { test, expect } from "bun:test"; test("snapshot with dynamic values", () => { const user = { id: Math.random(), name: "John", createdAt: new Date().toISOString(), }; expect(user).toMatchSnapshot({ id: expect.any(Number), createdAt: expect.any(String), }); }); ``` The snapshot file stores `Any<Type>` for the matched properties.

Write snapshot tests for snapshots

Import `test` and `expect` from "bun:test" to write snapshot tests.

Property matchers in snapshots

For values that change between test runs like timestamps or IDs, use property matchers. Pass a second argument to `.toMatchSnapshot()` with property matchers like `expect.any(Number)` or `expect.any(String)`. In the snapshot file, these are stored as `Any<Type>` (for example, `Any<Number>`).

toMatchSnapshot matcher for basic snapshots

The `.toMatchSnapshot()` matcher saves the output of a value and compares it against future test runs. On the first run, Bun serializes the argument to `expect` and writes it to a snapshot file in a `__snapshots__` directory alongside the test file.

Snapshot file location and format

Bun creates a `__snapshots__` directory alongside the test file. For a test file named `snap.test.ts`, Bun creates `__snapshots__/snap.test.ts.snap`. The snapshot file begins with `// Bun Snapshot v1, https://bun.sh/docs/test/snapshots` and stores snapshots as `exports[`test name N`] = `value`;`.

Update snapshots command

Regenerate snapshots with `bun test --update-snapshots`. This should be used when you have intentionally changed the output. In CI environments, Bun does not write new snapshots unless you pass the `--update-snapshots` flag.

toMatchInlineSnapshot matcher

The `.toMatchInlineSnapshot()` matcher stores inline snapshots directly in your test file instead of in a separate file. On the first run, Bun automatically updates the test file to insert the snapshot as a template string argument. On subsequent runs, Bun compares the value against the inline snapshot.

Snapshot matchers in Bun

Snapshot matchers available: `.toMatchSnapshot()`, `.toMatchInlineSnapshot()`, `.toThrowErrorMatchingSnapshot()`, `.toThrowErrorMatchingInlineSnapshot()`

Snapshot matchers not yet implemented

The `.addSnapshotSerializer()` matcher is not yet implemented in Bun.

Give your agent this brain