jest/no-interpolation-in-snapshots

Disallow ${} in inline snapshots

An inline snapshot is text that the test runner 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 the runner can't keep the ${} when it rewrites the snapshot.

The rule checks the template literals passed to toMatchInlineSnapshot and toThrowErrorMatchingInlineSnapshot.

Configure#

Off by default. To turn it on:

bunfig.toml
[lint.rules]
"jest/no-interpolation-in-snapshots" = "error" # or "warn"

Examples#

example.ts1 problem
import { expect, test } from "@jest/globals";

test("creates a user", () => {
  const user = createUser("Ada");
  expect(user).toMatchInlineSnapshot(`
    {
      "id": ${user.id},
      "name": "Ada",
    }
  `);
});
example.ts1 problem
import { expect, test } from "@jest/globals";

test("rejects a missing file", () => {
  expect(() => load(path)).toThrowErrorMatchingInlineSnapshot(`"cannot read ${path}"`);
});

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 "@jest/globals";

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