vitest-dev/vitest · error · Error
cannot read when saving inline snapshot
Error message
cannot read ${file} when saving inline snapshot What it means
While saving inline snapshots, saveInlineSnapshots reads each affected source file via environment.readSnapshotFile; if that returns null (file missing, unreadable, or the environment cannot read it), the writer cannot compute offsets to insert/replace snapshots and aborts.
Defensive patterns
Strategy: try-catch
Validate before calling
// Pre-check the source file is readable before running inline snapshots
const code = await environment.readSnapshotFile(file)
if (code == null) throw new Error(`source file ${file} is not readable; cannot save inline snapshot`) Try / catch
try {
await saveInlineSnapshots(environment, snapshots)
} catch (e) {
if (e instanceof Error && /cannot read .* when saving inline snapshot/.test(e.message)) {
// restore/recreate the file, or write snapshots manually
} else throw e
} Prevention
- Ensure source files exist and are readable for the whole test run
- Implement readSnapshotFile to return string contents in custom environments
- Avoid moving/deleting test files mid-run
When it happens
Trigger: An inline snapshot assertion whose source file was deleted, moved, or made unreadable between the test run and the save phase; a custom SnapshotEnvironment whose readSnapshotFile returns null; permission error on the source file.
Common situations: File deleted/renamed mid-run; running tests in a read-only or sandboxed FS where the environment cannot read the source; custom environment with a broken readSnapshotFile; race where another process moved the file.
Related errors
- Snapshot file " " does not exist.
- @vitest/snapshot: Couldn't infer stack frame for inline…
- Access denied to " ". See Vite config documentation for…
- Access denied to " ". See Vite config documentation for…
- aria adapter expects an Element
AI-assisted analysis of vitest-dev/vitest@1fa9837ec2 (2026-08-11).
Data as JSON: /api/errors/f86468bc07d99a64.
Report an issue: GitHub.
Appendix: source
Thrown at packages/snapshot/src/port/inlineSnapshot.ts:34
column: number
// it maybe possible to accurately extract this from `ParsedStack.method`,
// but for now, we ask higher level assertion to pass it explicitly
// since this is useful for certain error messages before we extract stack.
assertionName?: string
}
export async function saveInlineSnapshots(
environment: SnapshotEnvironment,
snapshots: Array<InlineSnapshot>,
): Promise<void> {
const MagicString = (await import('magic-string')).default
const files = new Set(snapshots.map(i => i.file))
await Promise.all(
Array.from(files).map(async (file) => {
const snaps = snapshots.filter(i => i.file === file)
const code = await environment.readSnapshotFile(file)
if (code == null) {
throw new Error(`cannot read ${file} when saving inline snapshot`)
}
const s = new MagicString(code)
for (const snap of snaps) {
const index = positionToOffset(code, snap.line, snap.column)
replaceInlineSnap(code, s, index, snap.snapshot, snap.assertionName)
}
const transformed = s.toString()
if (transformed !== code) {
await environment.saveSnapshotFile(file, transformed)
}
}),
)
}
const defaultStartObjectRegexView on GitHub (pinned to 1fa9837ec2)