Partial snapshot state after an interrupted Vitest update run
0 reputation · 26 Mar 2021, 00:27 UTC
0 reputation · 26 Mar 2021, 00:27 UTC
Determine whether Vitest offers a documented way to restore snapshot files to a consistent state after an interrupted snapshot update or failed upgrade.
Vitest writes snapshot files sequentially when --update is enabled. The documentation does not guarantee atomic writes or provide a rollback command. If the process is killed during an update, some snapshot files may be missing or partially written while others remain updated. This behavior is based on Vitest v1.x snapshot handling; future releases may change it.
Specific questions:
34775 reputation · 26 Mar 2021, 06:55 UTC
Vitest does not provide a built-in atomic update mechanism or a native rollback command for snapshots. Because snapshot files are written to disk sequentially as tests complete, an interrupted process (such as a SIGKILL or a system crash) during a --update run will leave the __snapshots__ directory in an inconsistent state.
Based on Vitest v1.x behavior, the following outcomes are expected after an interrupted update:
Since Vitest lacks an internal recovery tool, the recommended approach is to use your version control system (VCS) to restore the snapshot directory to the last known-good state before attempting a fresh update.
git checkout HEAD -- **/ __snapshots__/*
npx vitest run
npx vitest run --update
This guidance assumes you are using Vitest v1.x and that your snapshots are tracked in a Git repository. If snapshots are not versioned, the only recovery option is to manually delete the __snapshots__ directory and regenerate them entirely, though this risks introducing regressions if the current code contains unintended changes.
Diagnostic Detail Required: Are you seeing specific SyntaxError or Unexpected token messages when running tests after the interruption? If so, this confirms file corruption rather than simple version mismatch.
Use comments to ask for clarification. Post a solution as an answer.
34,775 reputation · 26 Mar 2021, 11:34 UTC
One clarification that helps with triage: Vitest distinguishes a missing snapshot file from a malformed one. A missing __snapshots__ file is treated as a new snapshot that will be created on the next run, whereas a partially written file must still be loaded as a JavaScript module before comparison can happen.
With Vitest v1.x snapshot handling, that means truncation or invalid syntax typically surfaces as a parse error for the whole test file rather than a silent skip. Because writes happen sequentially during --update and there is no documented atomic write or --rollback-snapshots flag, an interrupted run can leave a mix of updated, unchanged and corrupted files.
Verification is version-sensitive, so confirming behavior against the installed Vitest release and checking which snapshot paths are affected by custom resolveSnapshotPath is prudent before bulk deletion.