vitest-dev/vitest · error · Error
@vitest/snapshot: Couldn't infer stack frame for inline…
Error message
@vitest/snapshot: Couldn't infer stack frame for inline snapshot.\n${message} What it means
When recording/updating an inline snapshot, SnapshotState._resolveInlineStack parses the assertion's Error stack and calls _inferInlineSnapshotStack to find the frame pointing at the assertion call site. If no suitable frame can be inferred (e.g. stack empty, all frames filtered, source maps unmapped), it cannot compute the line/column to write the snapshot into and throws, listing the candidate frames for diagnosis.
Defensive patterns
Strategy: fallback
Validate before calling
// Before recording, sanity-check that the error has a usable stack
if (!error.stack && !error.stackTrace) throw new Error('cannot infer inline snapshot frame: error has no stack') Type guard
function hasUsableStack(e: Error): boolean { try { return parseErrorStacktrace(e, { ignoreStackEntries: [] }).length > 0 } catch { return false } } Try / catch
try {
// assertion that records inline snapshot
} catch (e) {
if (e instanceof Error && /Couldn't infer stack frame for inline snapshot/.test(e.message)) {
// fall back: write the snapshot manually with toMatchInlineSnapshot('...')
} else throw e
} Prevention
- Generate source maps in test builds (sourcemap: true)
- Avoid minifying test bundles
- Keep ignoreStackEntries permissive for snapshot frames
- Manually populate inline snapshots when stacks are unreliable
When it happens
Trigger: Inline snapshot (toMatchInlineSnapshot) update where the error stack is empty or all frames are ignored; source maps missing or broken so parsed frames do not match the source file; stack-stripping/minification removing the relevant frame; running in an environment that does not capture real stack traces.
Common situations: Bundled/transpiled code with broken source maps; custom stack-trace parser filtering too aggressively; minified production build of tests; runtime without Error.captureStackTrace; async boundary that lost the original frame.
Related errors
- cannot read when saving inline snapshot
- aria adapter expects an Element
- Assertion name is not set. This is a bug in Vitest. Please…
- expect.poll() is not supported in combination with .
- expected must be a function, received
AI-assisted analysis of vitest-dev/vitest@1fa9837ec2 (2026-08-11).
Data as JSON: /api/errors/d8cc49643d9f2b77.
Report an issue: GitHub.
Appendix: source
Thrown at packages/snapshot/src/port/state.ts:272
}
private _resolveInlineStack(options: {
testId: string
snapshot: string
assertionName: string
error: Error
}): ParsedStack {
const { testId, snapshot, assertionName, error } = options
const stacks = parseErrorStacktrace(
error,
{ ignoreStackEntries: [] },
)
const _stack = this._inferInlineSnapshotStack(stacks)
if (!_stack) {
const message = stacks.map(s =>
` ${s.file}:${s.line}:${s.column}${s.method ? ` (${s.method})` : ''}`,
).join('\n')
throw new Error(
`@vitest/snapshot: Couldn't infer stack frame for inline snapshot.\n${message}`,
)
}
const stack = this.environment.processStackTrace?.(_stack) || _stack
// removing 1 column, because source map points to the wrong
// location for js files, but `column-1` points to the same in both js/ts
// https://github.com/vitejs/vite/issues/8657
stack.column--
// reject multiple inline snapshots at the same location if snapshot is different
const snapshotsWithSameStack = this._inlineSnapshotStacks.filter(s => isSameStackPosition(s, stack))
if (snapshotsWithSameStack.length > 0) {
// ensure only one snapshot will be written at the same location
this._inlineSnapshots = this._inlineSnapshots.filter(s => !isSameStackPosition(s, stack))
const differentSnapshot = snapshotsWithSameStack.find(s => s.snapshot !== snapshot)
if (differentSnapshot) {
throw Object.assign(View on GitHub (pinned to 1fa9837ec2)