Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(snapshot): fix obsoleteness check of toMatchSnapshot("...") #7126

Merged
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 @@ -109,7 +109,10 @@ export default class SnapshotState {

markSnapshotsAsCheckedForTest(testName: string): void {
this._uncheckedKeys.forEach((uncheckedKey) => {
if (keyToTestName(uncheckedKey) === testName) {
// skip snapshots with following keys
// testName n
// testName > xxx n (this is for toMatchSnapshot("xxx") API)
if (/ \d+$| > /.test(uncheckedKey.slice(testName.length))) {
this._uncheckedKeys.delete(uncheckedKey)
}
})
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html

exports[`custom a > x 1`] = `0`;

exports[`custom a > y 1`] = `0`;

exports[`custom b > w 1`] = `0`;

exports[`custom b > z 1`] = `0`;
11 changes: 11 additions & 0 deletions test/snapshots/test/fixtures/skip-test-custom/basic.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { expect, test } from 'vitest';

test('custom a', () => {
expect(0).toMatchSnapshot('x');
expect(0).toMatchSnapshot('y');
});

test('custom b', () => {
expect(0).toMatchSnapshot('z');
expect(0).toMatchSnapshot('w');
});
66 changes: 66 additions & 0 deletions test/snapshots/test/skip-test.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import fs from 'node:fs'
import path from 'node:path'
import { expect, test } from 'vitest'
import { runVitest } from '../../test-utils'

Expand Down Expand Up @@ -46,3 +47,68 @@ test('snapshots in skipped test/suite is not obsolete', async () => {
"
`)
})

test('handle obsoleteness of toMatchSnapshot("custom message")', async () => {
const root = path.join(import.meta.dirname, './fixtures/skip-test-custom')

// clear snapshots
fs.rmSync(path.join(root, '__snapshots__'), { recursive: true, force: true })

// create snapshot on first run
let vitest = await runVitest({
root,
update: true,
})
expect(vitest.stdout).toContain('Snapshots 4 written')
expect(fs.readFileSync(path.join(root, '__snapshots__/basic.test.ts.snap'), 'utf-8')).toMatchInlineSnapshot(`
"// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html

exports[\`custom a > x 1\`] = \`0\`;

exports[\`custom a > y 1\`] = \`0\`;

exports[\`custom b > w 1\`] = \`0\`;

exports[\`custom b > z 1\`] = \`0\`;
"
`)

// Skipped tests' `toMatchSnapshot("...")` is not considered obsolete
vitest = await runVitest({
root,
testNamePattern: 'custom a',
})
expect(vitest.stdout).toContain('1 passed')
expect(vitest.stdout).toContain('1 skipped')
expect(vitest.stdout).not.toContain('obsolete')

vitest = await runVitest({
root,
testNamePattern: 'custom b',
})
expect(vitest.stdout).toContain('1 passed')
expect(vitest.stdout).toContain('1 skipped')
expect(vitest.stdout).not.toContain('obsolete')

// check snapshot doesn't change when skip + update
vitest = await runVitest({
root,
update: true,
testNamePattern: 'custom a',
})
expect(vitest.stdout).toContain('1 passed')
expect(vitest.stdout).toContain('1 skipped')
expect(vitest.stdout).not.toContain('obsolete')
expect(fs.readFileSync(path.join(root, '__snapshots__/basic.test.ts.snap'), 'utf-8')).toMatchInlineSnapshot(`
"// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html

exports[\`custom a > x 1\`] = \`0\`;

exports[\`custom a > y 1\`] = \`0\`;

exports[\`custom b > w 1\`] = \`0\`;

exports[\`custom b > z 1\`] = \`0\`;
"
`)
})
Loading