Skip to content
Latchkey

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.

Jest output
● 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.

Terminal
# 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; done

Common 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

  1. Run the test locally with the update flag to create the snapshot.
  2. Review the generated __snapshots__ file.
  3. Commit it so CI has a baseline to compare against.
Terminal
npx jest -u
git add **/__snapshots__/*.snap

Keep snapshot files in version control

Ensure .gitignore does not exclude __snapshots__, or CI will never see the committed baseline.

.gitignore
# .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 TZ and LANG in 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 -u and review diffs before pushing snapshot changes.
  • Do not gitignore the __snapshots__ directory.

Frequently asked questions

What causes Jest "New snapshot was not written" (--ci flag) in CI?
There are 2 common causes: a snapshot was never committed and ci runs jest with --ci (often implicitly). 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.
How do I fix Jest "New snapshot was not written" (--ci flag) in CI?
There are 2 fixes depending on which cause you have: generate and commit the snapshot locally and keep snapshot files in version control. Work through them in order, since the first is the most common.
What does Jest "New snapshot was not written" (--ci flag) in CI actually mean?
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.
How do I stop Jest "New snapshot was not written" (--ci flag) in CI happening again?
Commit snapshot files alongside the code that produces them. The prevention section lists 3 changes that keep it from recurring.

Related guides

References

Not every red build is your code. Latchkey repairs the ones that are not, on the runner. Start free → 30-day trial · No credit card