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
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327import fs, { readFileSync } from 'node:fs'import { join } from 'node:path'import { expect, test } from 'vitest'import { editFile, runVitest } from '../../test-utils'
const INLINE_BLOCK_RE = /\/\/ -- TEST INLINE START --\n([\s\S]*?)\/\/ -- TEST INLINE END --/g
function extractInlineBlocks(content: string): string { return Array.from(content.matchAll(INLINE_BLOCK_RE), (m) => m[1].trim()).join('\n\n')}
test('toMatchSnapshot and toMatchInlineSnapshot with properties', async () => { const root = join(import.meta.dirname, 'fixtures/properties') const testFile = join(root, 'basic.test.ts') const snapshotFile = join(root, '__snapshots__/basic.test.ts.snap')
// remove snapshots fs.rmSync(join(root, '__snapshots__'), { recursive: true, force: true }) editFile(testFile, (s) => s.replace(/toMatchInlineSnapshot\((\{[^}]*\}),\s*`[^`]*`\)/g, 'toMatchInlineSnapshot($1)'), )
// create snapshots from scratch let result = await runVitest({ root, update: 'new' }) expect(result.stderr).toMatchInlineSnapshot(`""`) expect(readFileSync(snapshotFile, 'utf-8')).toMatchInlineSnapshot(` "// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html
exports[\`file 1\`] = \` { "age": Any<Number>, "name": "alice", } \`;
exports[\`file asymmetric 1\`] = \` { "name": "bob", "score": toSatisfy<[Function lessThan100]>, } \`;
exports[\`file snapshot-only 1\`] = \` { "age": Any<Number>, "name": "dave", } \`; " `) expect(extractInlineBlocks(readFileSync(testFile, 'utf-8'))).toMatchInlineSnapshot(` "test("inline", () => { expect({ name: "carol", age: 25 }).toMatchInlineSnapshot({ age: expect.any(Number) }, \` { "age": Any<Number>, "name": "carol", } \`); });" `) expect(result.errorTree()).toMatchInlineSnapshot(` { "basic.test.ts": { "file": "passed", "file asymmetric": "passed", "file snapshot-only": "passed", "inline": "passed", }, } `)
// verify idempotency — re-run without update passes cleanly const result2 = await runVitest({ root, update: 'none' }) expect(result2.stderr).toMatchInlineSnapshot(`""`) expect(result2.errorTree()).toEqual(result.errorTree())
// edit tests to break properties check editFile(testFile, (s) => s .replace('age: 30', "age: 'thirty'") .replace('score: 95', 'score: 999') .replace('name: "dave"', 'name: "dave-edit"') .replace('age: 25', "age: 'twenty-five'"), )
// properties mismatch should NOT cause false-positive obsolete snapshot result = await runVitest({ root, update: 'none' }) expect(result.stderr).toMatchInlineSnapshot(` " ⎯⎯⎯⎯⎯⎯⎯ Failed Tests 4 ⎯⎯⎯⎯⎯⎯⎯
FAIL basic.test.ts > file Error: Snapshot properties mismatched
- Expected + Received
{ - "age": Any<Number>, + "age": "thirty", + "name": "alice", }
❯ basic.test.ts:4:44 2| 3| test("file", () => { 4| expect({ name: "alice", age: 'thirty' }).toMatchSnapshot({ age: expe… | ^ 5| }); 6|
⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯[1/4]⎯
FAIL basic.test.ts > file asymmetric Error: Snapshot properties mismatched
- Expected + Received
{ - "score": toSatisfy<[Function lessThan100]>, + "name": "bob", + "score": 999, }
❯ basic.test.ts:8:39 6| 7| test("file asymmetric", () => { 8| expect({ name: "bob", score: 999 }).toMatchSnapshot({ | ^ 9| score: expect.toSatisfy(function lessThan100(n) { 10| return n < 100;
⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯[2/4]⎯
FAIL basic.test.ts > file snapshot-only Error: Snapshot \`file snapshot-only 1\` mismatched
- Expected + Received
{ "age": Any<Number>, - "name": "dave", + "name": "dave-edit", }
❯ basic.test.ts:16:42 14| 15| test("file snapshot-only", () => { 16| expect({ name: "dave-edit", age: 42 }).toMatchSnapshot({ age: expect… | ^ 17| }); 18|
⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯[3/4]⎯
FAIL basic.test.ts > inline Error: Snapshot properties mismatched
- Expected + Received
{ - "age": Any<Number>, + "age": "twenty-five", + "name": "carol", }
❯ basic.test.ts:21:49 19| // -- TEST INLINE START -- 20| test("inline", () => { 21| expect({ name: "carol", age: 'twenty-five' }).toMatchInlineSnapshot(… | ^ 22| { 23| "age": Any<Number>,
⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯[4/4]⎯
" `) expect(result.errorTree()).toMatchInlineSnapshot(` { "basic.test.ts": { "file": [ "Snapshot properties mismatched", ], "file asymmetric": [ "Snapshot properties mismatched", ], "file snapshot-only": [ "Snapshot \`file snapshot-only 1\` mismatched", ], "inline": [ "Snapshot properties mismatched", ], }, } `)
// run with update — properties errors persist result = await runVitest({ root, update: 'all' }) expect(result.stderr).toMatchInlineSnapshot(` " ⎯⎯⎯⎯⎯⎯⎯ Failed Tests 3 ⎯⎯⎯⎯⎯⎯⎯
FAIL basic.test.ts > file Error: Snapshot properties mismatched
- Expected + Received
{ - "age": Any<Number>, + "age": "thirty", + "name": "alice", }
❯ basic.test.ts:4:44 2| 3| test("file", () => { 4| expect({ name: "alice", age: 'thirty' }).toMatchSnapshot({ age: expe… | ^ 5| }); 6|
⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯[1/3]⎯
FAIL basic.test.ts > file asymmetric Error: Snapshot properties mismatched
- Expected + Received
{ - "score": toSatisfy<[Function lessThan100]>, + "name": "bob", + "score": 999, }
❯ basic.test.ts:8:39 6| 7| test("file asymmetric", () => { 8| expect({ name: "bob", score: 999 }).toMatchSnapshot({ | ^ 9| score: expect.toSatisfy(function lessThan100(n) { 10| return n < 100;
⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯[2/3]⎯
FAIL basic.test.ts > inline Error: Snapshot properties mismatched
- Expected + Received
{ - "age": Any<Number>, + "age": "twenty-five", + "name": "carol", }
❯ basic.test.ts:21:49 19| // -- TEST INLINE START -- 20| test("inline", () => { 21| expect({ name: "carol", age: 'twenty-five' }).toMatchInlineSnapshot(… | ^ 22| { 23| "age": Any<Number>,
⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯[3/3]⎯
" `) expect(readFileSync(snapshotFile, 'utf-8')).toMatchInlineSnapshot(` "// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html
exports[\`file 1\`] = \` { "age": Any<Number>, "name": "alice", } \`;
exports[\`file asymmetric 1\`] = \` { "name": "bob", "score": toSatisfy<[Function lessThan100]>, } \`;
exports[\`file snapshot-only 1\`] = \` { "age": Any<Number>, "name": "dave-edit", } \`; " `) expect(extractInlineBlocks(readFileSync(testFile, 'utf-8'))).toMatchInlineSnapshot(` "test("inline", () => { expect({ name: "carol", age: 'twenty-five' }).toMatchInlineSnapshot({ age: expect.any(Number) }, \` { "age": Any<Number>, "name": "carol", } \`); });" `) expect(result.errorTree()).toMatchInlineSnapshot(` { "basic.test.ts": { "file": [ "Snapshot properties mismatched", ], "file asymmetric": [ "Snapshot properties mismatched", ], "file snapshot-only": "passed", "inline": [ "Snapshot properties mismatched", ], }, } `)})