bun:test/no-interpolation-in-snapshots
Disallow ${} in inline snapshots
An inline snapshot is text that bun test writes into your file and rewrites when you update snapshots. With ${} in it, the expected text is computed while the test runs. The snapshot no longer shows what the output looks like, and bun test --update-snapshots can't rewrite it.
The rule checks the template literals passed to toMatchInlineSnapshot and toThrowErrorMatchingInlineSnapshot.
Configure#
Off by default. To turn it on:
bunfig.toml
[lint.rules]
"bun:test/no-interpolation-in-snapshots" = "error" # or "warn"Examples#
example.ts1 problem
import { expect, test } from "bun:test";
test("creates a user", () => {
const user = createUser("Ada");
expect(user).toMatchInlineSnapshot(`Do not use string interpolation inside of snapshots
{
"id": ${user.id},
"name": "Ada",
}
`);
});example.ts1 problem
import { expect, test } from "bun:test";
test("rejects a missing file", () => {
expect(() => load(path)).toThrowErrorMatchingInlineSnapshot(`"cannot read ${path}"`Do not use string interpolation inside of snapshots);
});Not reported. For a value that changes from run to run, pass a property matcher as the first argument and keep the snapshot plain text:
example.tsNo problems
import { expect, test } from "bun:test";
test("creates a user", () => {
const user = createUser("Ada");
expect(user).toMatchInlineSnapshot({ id: expect.any(Number) }, `
{
"id": Any<Number>,
"name": "Ada",
}
`);
});