Skip to content

Commit

Permalink
fix(snapshot): fix obsoleteness check of toMatchSnapshot("...") (#7126
Browse files Browse the repository at this point in the history
)
  • Loading branch information
hi-ogawa authored Jan 7, 2025
1 parent 74dbe03 commit ac9ba15
Show file tree
Hide file tree
Showing 4 changed files with 90 additions and 1 deletion.
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\`;
"
`)
})

0 comments on commit ac9ba15

Please sign in to comment.