{"id":"c852741ce5060bc3","repo":"jestjs/jest","slug":"jest-couldn-t-infer-stack-frame-for-inline-snapsh","errorCode":null,"errorMessage":"Jest: Couldn't infer stack frame for inline snapshot.","messagePattern":"Jest: Couldn't infer stack frame for inline snapshot\\.","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"packages/jest-snapshot/src/State.ts","lineNumber":125,"sourceCode":"      }\n    }\n  }\n\n  private _addSnapshot(\n    key: string,\n    receivedSerialized: string,\n    options: {isInline: boolean; error?: Error},\n  ): void {\n    this._dirty = true;\n    if (options.isInline) {\n      // eslint-disable-next-line unicorn/error-message\n      const error = options.error || new Error();\n      const lines = getStackTraceLines(\n        removeLinesBeforeExternalMatcherTrap(error.stack || ''),\n      );\n      const frame = getTopFrame(lines);\n      if (!frame) {\n        throw new Error(\n          \"Jest: Couldn't infer stack frame for inline snapshot.\",\n        );\n      }\n      this._inlineSnapshots.push({\n        frame,\n        snapshot: receivedSerialized,\n      });\n    } else {\n      this._snapshotData[key] = receivedSerialized;\n    }\n  }\n\n  clear(): void {\n    this._snapshotData = this._initialData;\n    this._inlineSnapshots = [];\n    this._counters = new Map();\n    this._index = 0;\n    this.added = 0;","sourceCodeStart":107,"sourceCodeEnd":143,"githubUrl":"https://github.com/jestjs/jest/blob/f49721c78e195558b40913977c9230f5b7f559d8/packages/jest-snapshot/src/State.ts#L107-L143","documentation":"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.","triggerScenarios":"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).","commonSituations":"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.","solutions":["Ensure `Error.stackTraceLimit` is at its default (10+) in the test environment.","Avoid overriding `Error.prepareStackTrace` in setup files.","Use `toMatchSnapshot` (non-inline) instead, which doesn't need a frame.","If you're driving the matcher programmatically, pass a real `error` with a usable stack via the matcher context."],"exampleFix":"// before — somewhere in setup\nError.stackTraceLimit = 0;\nexpect(x).toMatchInlineSnapshot(); // throws\n\n// after\nError.stackTraceLimit = 10;\nexpect(x).toMatchInlineSnapshot();","handlingStrategy":"validation","validationCode":"if (Error.stackTraceLimit < 10) {\n  Error.stackTraceLimit = 10;\n}\nconst probe = new Error();\nif (!probe.stack) {\n  throw new Error('Stack traces unavailable; inline snapshots will not work here.');\n}","typeGuard":null,"tryCatchPattern":"try {\n  expect(x).toMatchInlineSnapshot();\n} catch (e) {\n  if (e instanceof Error && /Couldn't infer stack frame/.test(e.message)) {\n    // fall back to toMatchSnapshot (non-inline)\n    expect(x).toMatchSnapshot();\n  } else throw e;\n}","preventionTips":["Don't set `Error.stackTraceLimit = 0` or override `Error.prepareStackTrace` in test setup.","Prefer non-inline snapshots in environments with non-standard stacks.","Audit setup files for stack-trace manipulation."],"tags":["inline-snapshot","stack-trace","matcher"],"analyzedSha":"f49721c78e195558b40913977c9230f5b7f559d8","analyzedAt":"2026-08-03T20:16:28.571Z","schemaVersion":2}