Jest "New snapshot was not written" (--ci flag) in CI
When Jest runs with --ci, it will not write new snapshots automatically. A test that has no committed snapshot fails instead of silently creating one, so missing snapshots cannot pass review unnoticed.
What this error means
A snapshot test fails with "New snapshot was not written: The update flag must be explicitly passed to write a new snapshot." The same test passes locally because local runs auto-create snapshots.
● MyComponent › renders
New snapshot was not written: The update flag must be explicitly passed to write a
new snapshot.
This is likely because this test is run in a continuous integration (CI) environment
in which snapshots are not written by default.Diagnose it: flake, environment, or genuine failure?
Before debugging the assertion, establish whether the test is deterministic. A test that fails only in CI is usually order-dependent, time-dependent, or racing something, and fixing the assertion will not help.
# does it fail in isolation?
npx vitest run path/to/file.test.ts
# is it order dependent? run the suite in a random order twice
npx vitest run --sequence.shuffle
# is it a race? run the same file repeatedly
for i in $(seq 1 20); do npx vitest run path/to/file.test.ts || break; doneCommon causes
A snapshot was never committed
The test creates a new snapshot, but the __snapshots__ file was not committed, so under --ci there is nothing to compare against and nothing is written.
CI runs Jest with --ci (often implicitly)
Jest treats a CI environment as --ci automatically, disabling snapshot creation so the run cannot pass by writing fresh snapshots.
How to fix it
Generate and commit the snapshot locally
- Run the test locally with the update flag to create the snapshot.
- Review the generated
__snapshots__file. - Commit it so CI has a baseline to compare against.
npx jest -u
git add **/__snapshots__/*.snapKeep snapshot files in version control
Ensure .gitignore does not exclude __snapshots__, or CI will never see the committed baseline.
# .gitignore should NOT contain:
# __snapshots__/CI-only causes worth ruling out
- Runners have fewer cores than a laptop, so timing-sensitive tests that pass locally fail under contention.
- No TTY and a different locale or timezone. Snapshot tests containing formatted dates or numbers are the usual casualty; pin
TZandLANGin the job. - Parallel workers sharing a database, a port, or a temp directory. Give each worker its own namespace.
- Default timeouts calibrated on a fast machine. A cold runner is slower on first execution, especially before any cache warms.
How to prevent it
- Commit snapshot files alongside the code that produces them.
- Run
jest -uand review diffs before pushing snapshot changes. - Do not gitignore the
__snapshots__directory.