Skip to content
NewBun 1.4.5 is out. Release notes
AphrodyBun

Linter · Rules

bun:test/no-large-snapshots

Disallow snapshots that are longer than a set number of lines

Nobody reads a several-hundred-line snapshot in a review. When it changes, people update it without looking, and then the test passes no matter what the code does. A short snapshot, or an assertion on the few values that matter, still tells you something when it fails.

The rule counts the lines of the snapshot in toMatchInlineSnapshot and toThrowErrorMatchingInlineSnapshot. The limit is 50 lines. Snapshots in .snap files aren't checked.

Configure

Off by default. To turn it on:

bunfig.toml
[lint.rules]
"bun:test/no-large-snapshots" = "error" # or "warn"

Every option, set to its default. The ones that are commented out are unset by default.

bunfig.toml
[lint.rules]
"bun:test/no-large-snapshots" = ["error", {
  # inlineMaxSize = <number>,
  maxSize = 50,
}]

Examples

These examples set inlineMaxSize to 3 to stay short.

Reported:

import { expect, test } from "bun:test";

test("loads the user", () => {
  expect(loadUser(1)).toMatchInlineSnapshot(`
    {
      "email": "ada@example.com",
      "id": 1,
      "name": "Ada",
    }
  `);
});
import { expect, test } from "bun:test";

test("creates a user", () => {
  expect(createUser("Ada")).toMatchInlineSnapshot({ id: expect.any(Number) }, `
    {
      "email": null,
      "id": Any<Number>,
      "name": "Ada",
    }
  `);
});

Not reported:

import { expect, test } from "bun:test";

test("loads the user", () => {
  const user = loadUser(1);
  expect(user.name).toBe("Ada");
  expect(user.email).toMatchInlineSnapshot(`"ada@example.com"`);
});

Options

OptionDefaultWhat it does
inlineMaxSizethe value of maxSizeThe most lines an inline snapshot can have.
maxSize50The limit to use when inlineMaxSize isn't set.
bunfig.toml
[lint.rules]
"bun:test/no-large-snapshots" = ["error", { inlineMaxSize = 20 }]

Without inlineMaxSize, maxSize applies:

import { expect, test } from "bun:test";

test("lists the roles", () => {
  expect(roles()).toMatchInlineSnapshot(`
    [
      "admin",
      "editor",
      "viewer",
    ]
  `);
});