From 17e2b22dd018a9098a597d835bf45d1a7a3e73c8 Mon Sep 17 00:00:00 2001 From: hamed-bavar <54584195+hamed-bavar@users.noreply.github.com> Date: Wed, 9 Sep 2026 11:02:41 +0330 Subject: [PATCH] fix(snapshot): report obsolete keys next to skipped tests (fix #11157) (#11158) Co-authored-by: Hiroshi Ogawa <4232207+hi-ogawa@users.noreply.github.com> Co-authored-by: OpenCode (gpt-6-astra) --- packages/snapshot/src/port/state.ts | 5 +- test/e2e/snapshots/skip-test.test.ts | 91 +++++++++++++++++++++++++++- 2 files changed, 94 insertions(+), 2 deletions(-) diff --git a/packages/snapshot/src/port/state.ts b/packages/snapshot/src/port/state.ts index 2fec430b8..4c46373a4 100644 --- a/packages/snapshot/src/port/state.ts +++ b/packages/snapshot/src/port/state.ts @@ -150,7 +150,10 @@ export default class SnapshotState { // skip snapshots with following keys // testName n // testName > xxx n (this is for toMatchSnapshot("xxx") API) - if (/ \d+$| > /.test(uncheckedKey.slice(testName.length))) { + if ( + uncheckedKey.startsWith(testName) + && /^ \d+$|^ > /.test(uncheckedKey.slice(testName.length)) + ) { this._uncheckedKeys.delete(uncheckedKey) } }) diff --git a/test/e2e/snapshots/skip-test.test.ts b/test/e2e/snapshots/skip-test.test.ts index cd6ad0890..5252f05b9 100644 --- a/test/e2e/snapshots/skip-test.test.ts +++ b/test/e2e/snapshots/skip-test.test.ts @@ -1,7 +1,7 @@ import fs from 'node:fs' import path from 'node:path' import { expect, test } from 'vitest' -import { runVitest } from '../../test-utils' +import { runInlineTests, runVitest } from '../../test-utils' test('snapshots in skipped test/suite is not obsolete', async () => { // create snapshot on first run @@ -114,3 +114,92 @@ test('handle obsoleteness of toMatchSnapshot("custom message")', async () => { " `) }) + +test('obsolete snapshots are still reported when another test is skipped', async () => { + const structure = { + 'basic.test.ts': ` + import { expect, it } from 'vitest' + + it.skip('a', () => { + expect(0).toMatchSnapshot() + }) + + it('b', () => { + expect(1).toMatchSnapshot() + }) + `, + '__snapshots__/basic.test.ts.snap': `// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html + +exports[\`a 1\`] = \`0\`; + +exports[\`b 1\`] = \`1\`; + +exports[\`removed test 1\`] = \`2\`; +`, + } + + const failed = await runInlineTests(structure, { update: 'none' }) + expect(failed.errorTree()).toMatchInlineSnapshot(` + { + "basic.test.ts": { + "__module_errors__": [ + "Obsolete snapshots found when no snapshot update is expected. + · removed test 1 + ", + ], + "a": "skipped", + "b": "passed", + }, + } + `) + + const updated = await runInlineTests(structure, { update: 'all' }) + expect(fs.readFileSync(path.join(updated.root, '__snapshots__/basic.test.ts.snap'), 'utf-8')).toMatchInlineSnapshot(` + "// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html + + exports[\`a 1\`] = \`0\`; + + exports[\`b 1\`] = \`1\`; + " + `) +}) + +test('skipped test does not hide obsolete snapshots of a test with a longer name', async () => { + const result = await runInlineTests({ + 'basic.test.ts': ` + import { expect, it } from 'vitest' + + it.skip('foo', () => { + expect(0).toMatchSnapshot() + expect(0).toMatchSnapshot('custom') + }) + + it('bar', () => { + expect(1).toMatchSnapshot() + }) + `, + '__snapshots__/basic.test.ts.snap': `// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html + +exports[\`bar 1\`] = \`1\`; + +exports[\`foo 1\`] = \`0\`; + +exports[\`foo > custom 1\`] = \`0\`; + +exports[\`foobar 1\`] = \`0\`; +`, + }, { update: 'none' }) + expect(result.errorTree()).toMatchInlineSnapshot(` + { + "basic.test.ts": { + "__module_errors__": [ + "Obsolete snapshots found when no snapshot update is expected. + · foobar 1 + ", + ], + "bar": "passed", + "foo": "skipped", + }, + } + `) +}) -- 2.51.2