Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion packages/snapshot/src/port/state.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
})
Expand Down
91 changes: 90 additions & 1 deletion test/e2e/snapshots/skip-test.test.ts
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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",
},
}
`)
})
Loading