Skip to content

obsolete snapshot reported when snapshot fails with toMatchSnapshot(propertes) #9984

@hi-ogawa

Description

@hi-ogawa

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.2

Used Package Manager

npm

Validations

Metadata

Metadata

Assignees

No one assigned

    Labels

    p3-minor-bugAn edge case that only affects very specific usage (priority)

    Type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions