skip to content

A Jest 30 CI job keeps passing tests whose snapshots were never committed, writing them afresh on every run; why does that happen, and how do you stop it?

level: seniorimportance: should knowfreq 38%

answer

  1. the job is not in CI mode
  2. new, all and none
  3. detected from environment variables
  4. an explicit flag beats detection

basics

~20 s

Jest only refuses to write a missing snapshot in CI mode, which it detects from environment variables such as CI; if detection fails or -u is passed, the job writes and passes. Run jest --ci explicitly and never pass -u in CI.

solid answer

~50 s

Jest resolves snapshot writing to `new`, `all` or `none`. A normal run is `new`: a missing snapshot is written and the test **passes**. CI mode, set by `--ci` or detected automatically from environment variables such as `CI`, turns that into `none`, so the same test fails with `New snapshot was not written`. A job that silently writes snapshots is either running where detection failed, for example tests launched in a container or a wrapper script that does not forward `CI`, or has `CI=false` or `--ci=false` set, or carries `-u`, which overrides CI mode and rewrites failing snapshots as well. The fix is to put `--ci` in the CI test command, keep `-u` out of it, and confirm with `jest --ci --showConfig` that `updateSnapshot` is `none`. Then a forgotten `.snap` file fails the build instead of passing invisibly.

code

json · 6 lines
json
{
  "scripts": {
    "test": "jest",
    "test:ci": "jest --ci"
  }
}

go deeper

for a junior

Recall that a new snapshot is written and passes on the first local run, and that CI is supposed to refuse to write it.

for a middle

Explain the new, none and all modes and that --ci, or detection from environment variables such as CI, chooses between them.

for a senior

Diagnose a CI job that writes snapshots: check detection, CI=false, --ci=false and -u with --showConfig, then pin --ci in the command.

for a principal

Make strict snapshot checking a property of the pipeline, not of whatever the runner's environment happens to contain.

## The symptom A pull request adds a test with `toMatchSnapshot()` but forgets to commit the `.snap` file. Locally that is invisible: the first run writes the file. In CI, the expectation is that Jest **refuses** to write it and fails the test, because a snapshot that is created during the check has asserted nothing. Here the job stays green, and the next run writes the same file again in a fresh workspace. Nothing is being checked. ## How Jest decides whether to write Jest computes one run-wide setting, `updateSnapshot`, from two inputs: whether CI mode is on, and whether `-u` was passed. | CI mode | `-u` passed | `updateSnapshot` | Missing snapshot | Failing snapshot | |---|---|---|---|---| | off | no | `new` | written, test passes | test fails | | on | no | `none` | test fails | test fails | | on or off | yes | `all` | written, test passes | rewritten, test passes | Two consequences follow: - CI mode changes **only** what happens to a missing snapshot. A mismatch fails in every mode except `all`. - **`-u` overrides CI mode.** `jest --ci -u` resolves to `all`, the most permissive setting there is. With CI mode on, the missing snapshot fails with this message: ```plaintext 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. ``` ## Where CI mode comes from - **`--ci`** on the command line turns it on; `--ci=false` turns it off. - **Without the flag**, Jest's default for `ci` is the result of an environment check: it looks for variables that common CI systems set, `CI` above all. Setting `CI=false` explicitly turns detection off. That automatic default is the weak point. Detection reads the environment of the **process that runs Jest**, not of the pipeline. ## Why the job writes snapshots Work through the causes in this order: 1. **Detection failed.** Tests run inside a container, a remote shell or a wrapper script that does not pass `CI` through, or on a self-hosted runner configured without it. Jest sees a developer machine and resolves to `new`. 2. **Detection was switched off.** Something sets `CI=false` (often to silence an unrelated tool's warnings) or the script passes `--ci=false`. 3. **`-u` is in the command.** A test script such as `"test": "jest -u"`, or a CI step added to "fix" snapshot failures, resolves to `all`: missing snapshots are written and failing ones rewritten. `jest --showConfig`, run with the job's exact command and environment, prints the resolved `"ci"` and `"updateSnapshot"` values and settles which one it is. ## The fix - Put **`--ci` explicitly** in the CI test command, so the behaviour does not depend on what the environment happens to contain. - Keep **`-u` out of every CI path**, including shared `npm test` scripts that CI calls. - Have the job fail if the test run **modified tracked files**; a snapshot written or rewritten during a check then shows up as a failure rather than a silent change. - Confirm once with `jest --ci --showConfig` that `updateSnapshot` reads `none`. ## Why a written snapshot proves nothing A snapshot assertion has no expected value of its own; the stored text **is** the expectation. When the check itself writes that text: - the value being "verified" is the value that was just produced, so the assertion is true by construction; - a regression introduced in the same pull request is recorded as the expected output; - in a disposable CI workspace the written file is thrown away, so the next run repeats the exercise and the suite never accumulates a real baseline. That is why Jest's documentation recommends committing every snapshot and why the CI default is to refuse writing: new snapshots automatically pass, so they should not be allowed to pass a CI run. ## What `--ci` does not cover - It does not stop **obsolete** snapshots being reported; they already fail the run unless `-u` is passed. - It does not judge whether a committed snapshot is **meaningful**: a snapshot that someone recorded and committed without reading still passes. CI mode only guarantees that every snapshot the check compares against existed before the run started, rather than being written by the check itself.

  • What does Jest resolve updateSnapshot to when a CI script runs jest --ci -u?
    `all`. The update flag takes precedence over CI mode: Jest only resolves to `none` when CI mode is on **and** `-u` is absent. So `--ci -u` writes missing snapshots and rewrites every failing one, which is the opposite of what the `--ci` was meant to guarantee.
  • Why does a missing snapshot pass the test outside CI mode in Jest?
    In the default `new` mode Jest treats a missing snapshot as the first run of a new test: it serializes the value, writes the entry and reports the test as passed with `1 snapshot written`. That is convenient on a laptop and meaningless in CI, which is why CI mode refuses to write instead.

  • --ci makes a mismatched snapshot pass instead of fail
  • Adding -u alongside --ci keeps CI mode strict
  • Jest always knows it is in CI, whatever the environment
  • A missing snapshot fails the test on every machine
  • CI mode also refuses to compare existing snapshots