jestjs/jest · error · Error

Jest: Couldn't infer stack frame for inline snapshot.

Error message

Jest: Couldn't infer stack frame for inline snapshot.

What it means

Thrown by `SnapshotState._addSnapshot` (State.ts:118-128) when an inline snapshot can't be associated with a source location. The matcher uses the `Error.stack` captured at assertion time, runs it through `getStackTraceLines` + `removeLinesBeforeExternalMatcherTrap`, then `getTopFrame`; if no frame comes back the snapshot has nowhere to be written and Jest aborts.

Source

Thrown at packages/jest-snapshot/src/State.ts:125

      }
    }
  }

  private _addSnapshot(
    key: string,
    receivedSerialized: string,
    options: {isInline: boolean; error?: Error},
  ): void {
    this._dirty = true;
    if (options.isInline) {
      // eslint-disable-next-line unicorn/error-message
      const error = options.error || new Error();
      const lines = getStackTraceLines(
        removeLinesBeforeExternalMatcherTrap(error.stack || ''),
      );
      const frame = getTopFrame(lines);
      if (!frame) {
        throw new Error(
          "Jest: Couldn't infer stack frame for inline snapshot.",
        );
      }
      this._inlineSnapshots.push({
        frame,
        snapshot: receivedSerialized,
      });
    } else {
      this._snapshotData[key] = receivedSerialized;
    }
  }

  clear(): void {
    this._snapshotData = this._initialData;
    this._inlineSnapshots = [];
    this._counters = new Map();
    this._index = 0;
    this.added = 0;

View on GitHub (pinned to f49721c78e)

Solutions

  1. Ensure `Error.stackTraceLimit` is at its default (10+) in the test environment.
  2. Avoid overriding `Error.prepareStackTrace` in setup files.
  3. Use `toMatchSnapshot` (non-inline) instead, which doesn't need a frame.
  4. If you're driving the matcher programmatically, pass a real `error` with a usable stack via the matcher context.

Example fix

// before — somewhere in setup
Error.stackTraceLimit = 0;
expect(x).toMatchInlineSnapshot(); // throws

// after
Error.stackTraceLimit = 10;
expect(x).toMatchInlineSnapshot();
Defensive patterns

Strategy: validation

Validate before calling

if (Error.stackTraceLimit < 10) {
  Error.stackTraceLimit = 10;
}
const probe = new Error();
if (!probe.stack) {
  throw new Error('Stack traces unavailable; inline snapshots will not work here.');
}

Try / catch

try {
  expect(x).toMatchInlineSnapshot();
} catch (e) {
  if (e instanceof Error && /Couldn't infer stack frame/.test(e.message)) {
    // fall back to toMatchSnapshot (non-inline)
    expect(x).toMatchSnapshot();
  } else throw e;
}

Prevention

When it happens

Trigger: Calling `toMatchInlineSnapshot` from a context where `Error.stack` is stripped or non-standard (e.g. `Error.stackTraceLimit = 0`, certain VM/sandbox configurations, source-map-only stacks the parser doesn't understand).

Common situations: Test environments that override `Error.prepareStackTrace` or set `Error.stackTraceLimit = 0`. Running under transpilers that strip call-site info. Custom runner that invokes matchers outside a normal call stack.

Related errors


AI-assisted analysis of jestjs/jest@f49721c78e (2026-08-03). Data as JSON: /data/errors/c852741ce5060bc3.json. Report an issue: GitHub.