-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
obsolete snapshot reported when snapshot fails with toMatchSnapshot(propertes) #9984
Description
Describe the bug
(Spotted a pre-existing bug when writing a test case for #9973)
When using toMatchSnapshot(properties) with a properties argument, if the properties check fails (e.g. a literal value doesn't match), the snapshot is falsely reported as obsolete.
This happens because the properties check throws before snapshotState.match() is called, so the snapshot counter never increments for that key. Vitest then sees the snapshot key as unchecked and reports it as obsolete.
On --update, the "obsolete" snapshot gets deleted instead of updated.
repro.test.ts
import { expect, test } from 'vitest';
test('example', () => {
// initially snapshot created with
// { x: "hello" }
expect({ x: 1234 }).toMatchSnapshot({ x: expect.any(String) });
}); ❯ test/repro.test.ts (1 test | 1 failed) 6ms
× example 5ms
⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯ Failed Tests 1 ⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯
FAIL test/repro.test.ts > example
Error: Snapshot properties mismatched
- Expected
+ Received
{
- "x": Any<String>,
+ "x": 1234,
}
❯ test/repro.test.ts:4:23
2|
3| test('example', () => {
4| expect({ x: 1234 }).toMatchSnapshot({ x: expect.any(String) });
| ^
5| });
6|
❯ new Promise ../../../blitz.4c73681d.js:31:30606
⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯[1/1]⎯
Snapshots 1 obsolete
↳ test/repro.test.ts
· example 1
🤖 Root cause
In packages/snapshot/src/client.ts, SnapshotClient.assert() (and match()), the properties check happens before snapshotState.match():
// Properties check throws here...
if (!pass) {
throw createMismatchError('Snapshot properties mismatched', ...)
}
// ...so this is never reached, snapshot counter not incremented
const { actual, expected, key, pass } = snapshotState.match({ ... })The fix should ensure the snapshot key is "visited" (counter incremented) even when properties fail, so it's not flagged as obsolete.
Reproduction
https://stackblitz.com/edit/vitest-dev-vitest-ytnrbm3f?file=test%2Frepro.test.ts
System Info
System:
OS: Linux 5.0 undefined
CPU: (8) x64 Intel(R) Core(TM) i9-9880H CPU @ 2.30GHz
Memory: 0 Bytes / 0 Bytes
Shell: 1.0 - /bin/jsh
Binaries:
Node: 22.22.0 - /usr/local/bin/node
Yarn: 1.22.19 - /usr/local/bin/yarn
npm: 10.8.2 - /usr/local/bin/npm
pnpm: 8.15.6 - /usr/local/bin/pnpm
npmPackages:
@vitest/ui: latest => 4.1.2
vite: latest => 8.0.3
vitest: latest => 4.1.2Used Package Manager
npm
Validations
- Follow our Code of Conduct
- Read the Contributing Guidelines.
- Read the docs.
- Check that there isn't already an issue that reports the same bug to avoid creating a duplicate.
- Check that this is a concrete bug. For Q&A open a GitHub Discussion or join our Discord Chat Server.
- The provided reproduction is a minimal reproducible example of the bug.