jestjs / jest

Delightful JavaScript Testing.
https://jestjs.io
MIT License
43.84k stars 6.39k forks source link

[Bug]: Jest rewrites snapshots without changes if obsolete entries exist, even if the `--updateSnapshot` flag is not passed. #15120

Open iclanton opened 2 weeks ago

iclanton commented 2 weeks ago

Version

29.7.0

Steps to reproduce

  1. Introduce a test that contains two .toMatchSnapshot() tests
  2. Run jest -u
  3. Delete one of the snapshot tests
  4. Run jest under a debugger and observe that saveSnapshotFile is called:

https://github.com/jestjs/jest/blob/c54bccd657fb4cf060898717c09f633b4da3eec4/packages/jest-snapshot-utils/src/utils.ts#L138-L156

Expected behavior

Jest should not write snapshot files to the filesystem if the -u/--updateSnapshot flag is not provided.

Actual behavior

Jest writes to the filesystem.

Additional context

Build systems like BuildXL that monitor the filesystem for reads and writes to cache builds will raise issues if unexpected writes happen. In CI, Jest snapshots should never be written.

Environment

System:
    OS: macOS 14.5
    CPU: (8) arm64 Apple M2
  Binaries:
    Node: 20.10.0 - ~/.nvs/node/20.10.0/arm64/bin/node
    Yarn: 1.22.21 - ~/.nvs/node/20.10.0/arm64/bin/yarn
    npm: 10.2.3 - ~/.nvs/node/20.10.0/arm64/bin/npm
    pnpm: 8.12.0 - ~/.nvs/node/20.10.0/arm64/bin/pnpm