oxc-project/oxc · warning · OxcDiagnostic

Do not use string interpolation inside of snapshots

Error message

Do not use string interpolation inside of snapshots

What it means

Diagnostic from oxlint rule `jest/no-interpolation-in-snapshots` (shared with vitest) in crates/oxc_linter/src/rules/shared/jest_vitest/no_interpolation_in_snapshots.rs:11. Snapshot arguments must be static: when a template literal passed to a snapshot matcher contains `${}` interpolation, Jest refuses to update the snapshot. The rule tells you to remove the interpolation and overload the dynamic value with a matcher instead.

Source

Thrown at crates/oxc_linter/src/rules/shared/jest_vitest/no_interpolation_in_snapshots.rs:11

use oxc_ast::{AstKind, ast::Argument};
use oxc_diagnostics::OxcDiagnostic;
use oxc_span::Span;

use crate::{
    context::LintContext,
    utils::{PossibleJestNode, parse_expect_jest_fn_call},
};

fn no_interpolation_in_snapshots_diagnostic(span: Span) -> OxcDiagnostic {
    OxcDiagnostic::warn("Do not use string interpolation inside of snapshots")
        .with_help("Remove string interpolation from snapshots")
        .with_label(span)
}

pub const DOCUMENTATION: &str = r"### What it does

Prevents the use of string interpolations in snapshots.

### Why is this bad?

Interpolation prevents snapshots from being updated. Instead, properties should
be overloaded with a matcher by using
[property matchers](https://jestjs.io/docs/en/snapshot-testing#property-matchers).

### Examples

Examples of **incorrect** code for this rule:
```javascript

View on GitHub (pinned to e1e7af627c)

Solutions

  1. Move the dynamic value out of the snapshot and assert it separately: expect(x.timestamp).toEqual(expectedTs) alongside a static snapshot
  2. Replace the whole snapshot with targeted matchers (toMatchObject / toHaveProperty) when most of the content is dynamic
  3. Use a static descriptive string for the snapshot name and serialize dynamic parts into the object under a matcher placeholder (serializerAny in Vitest / expect.any in Jest)

Example fix

// before
expect(user).toMatchSnapshot(`user ${user.id}`);

// after
expect(user.id).toBe(42);
expect({ ...user, id: expect.any(Number) }).toMatchSnapshot();
Defensive patterns

Strategy: validation

Validate before calling

rg -n "(?:toMatch|toThrowErrorMatching)(?:Inline)?Snapshot\(\`[^\`]*\$\{" tests/

Prevention

When it happens

Trigger: An expect() snapshot call (parsed via parse_expect_jest_fn_call) whose Argument is a template literal containing substitutions, e.g. expect(x).toMatchSnapshot(`${Date.now()}`) or toThrowErrorMatchingSnapshot(`error ${code}`).

Common situations: Trying to embed timestamps, ids, or counts in snapshot names/bodies; migrating tests where inline values were templated; misunderstanding that external snapshots are keyed by static names and inline snapshots must be writable.

Related errors


AI-assisted analysis of oxc-project/oxc@e1e7af627c (2026-08-20). Data as JSON: /api/errors/f1631088d4d7808a. Report an issue: GitHub.