Something went wrong. Try again.
[READ-ONLY] Mirror of https://github.com/vitest-dev/vitest. Next generation testing framework powered by Vite. vitest.dev
test testing-tools vite
Something went wrong. Try again.
TypeScript
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214import fs from 'node:fs'import path from 'node:path'import { expect, test } from 'vitest'import { runInlineTests, runVitest } from '../../test-utils'
test('snapshots in skipped test/suite is not obsolete', async () => { // create snapshot on first run const root = path.join(import.meta.dirname, 'fixtures/skip-test')
fs.rmSync(path.join(root, '__snapshots__'), { recursive: true, force: true }) let vitest = await runVitest({ root, update: true, }) expect(vitest.stdout).toContain('Snapshots 2 written') expect(fs.readFileSync(path.join(root, '__snapshots__/repro.test.ts.snap'), 'utf-8')) .toMatchInlineSnapshot(` "// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html
exports[\`repro suite > inner case 1\`] = \`"hi-1"\`;
exports[\`top-level case 1\`] = \`"hi-2"\`; " `)
// running with `skipIf` enabled should not show "obsolete" vitest = await runVitest({ root, env: { ENABLE_SKIP: '1', }, }) expect(vitest.stdout).toContain('2 skipped') expect(vitest.stdout).not.toContain('obsolete')
// running with `skipIf` and `update` should keep snapshots vitest = await runVitest({ root, update: true, env: { ENABLE_SKIP: '1', }, }) expect(fs.readFileSync(path.join(root, '__snapshots__/repro.test.ts.snap'), 'utf-8')) .toMatchInlineSnapshot(` "// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html
exports[\`repro suite > inner case 1\`] = \`"hi-1"\`;
exports[\`top-level case 1\`] = \`"hi-2"\`; " `)})
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\`; " `)})
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", }, } `)})