This rule raises an issue when a built-in inline snapshot assertion (toMatchInlineSnapshot(), toThrowErrorMatchingInlineSnapshot()) contains a template literal with an unescaped interpolation.

Why is this an issue?

Inline snapshots are stored directly in the test source code. Test frameworks such as Jest and Vitest can update those snapshots automatically by rewriting the template literal passed to built-in inline snapshot matchers.

When that template literal contains an unescaped interpolation such as ${user.id}, the snapshot is no longer pure snapshot data. Part of the expected value comes from executable code, which makes the assertion harder to read and prevents snapshot updates from working reliably. Developers then have to maintain the snapshot manually, which defeats the point of using an inline snapshot.

This rule is intentionally conservative. It only raises an issue for built-in inline snapshot assertions that contain unescaped interpolations. Escaped placeholders such as \${user.id} are literal snapshot text and are not affected.

Keep inline snapshots fully static. When only part of a serialized object is expected to vary, prefer snapshot property matchers and keep the inline snapshot literal. If the snapshot must literally contain ${...} text, escape it as \${...}.

Code examples

Noncompliant code example

it("serializes the user", () => {
  const user = getUser();

  expect(user).toMatchInlineSnapshot(`
    {
      "id": ${user.id}, // Noncompliant: the snapshot interpolates user.id instead of a static expected value
      "name": "Alice",
    }
  `);
});

Compliant solution

it("serializes the user", () => {
  expect(getUser()).toMatchInlineSnapshot(
    { id: expect.any(Number) },
    `
    {
      "id": Any<Number>,
      "name": "Alice",
    }
    `
  );
});

Noncompliant code example

it("reports the invalid file", () => {
  const file = "config.yaml";

  expect(() => parseFile(file)).toThrowErrorMatchingInlineSnapshot(
    `"Invalid file: ${file}"` // Noncompliant: the snapshot interpolates the file variable instead of a static expected value
  );
});

Compliant solution

it("reports the invalid file", () => {
  expect(() => parseFile("config.yaml")).toThrowErrorMatchingInlineSnapshot(
    `"Invalid file: config.yaml"`
  );
});

Resources

Documentation