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.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777import fs, { readFileSync } from 'node:fs'import { join } from 'pathe'import { expect, test } from 'vitest'import { editFile, runVitest } from '../../test-utils'
// pnpm -C test/snapshots test:snaps inline-multiple-calls
test('same snapshots in single test', async () => { // pnpm -C test/snapshots test:fixtures --root test/fixtures/inline-multiple-calls same.test
// reset snapshot const root = join(import.meta.dirname, 'fixtures/inline-multiple-calls') const testFile = join(root, 'same.test.ts') editFile(testFile, (s) => s.replace(/toMatchInlineSnapshot\(`.*`\)/gs, 'toMatchInlineSnapshot()'))
// initial run (create snapshot) let vitest = await runVitest({ root, include: [testFile], update: true, }) expect(vitest.stderr).toBe('') expect(vitest.ctx?.snapshot.summary).toMatchInlineSnapshot(` { "added": 2, "didUpdate": true, "failure": false, "filesAdded": 1, "filesRemoved": 0, "filesRemovedList": [], "filesUnmatched": 0, "filesUpdated": 0, "matched": 0, "total": 2, "unchecked": 0, "uncheckedKeysByFile": [], "unmatched": 0, "updated": 0, } `) expect(fs.readFileSync(testFile, 'utf-8')).toContain( 'expect(value).toMatchInlineSnapshot(`"test1"`)', )
// no-update run vitest = await runVitest({ root, include: [testFile], update: false, }) expect(vitest.stderr).toBe('') expect(vitest.ctx?.snapshot.summary).toMatchInlineSnapshot(` { "added": 0, "didUpdate": false, "failure": false, "filesAdded": 0, "filesRemoved": 0, "filesRemovedList": [], "filesUnmatched": 0, "filesUpdated": 0, "matched": 2, "total": 2, "unchecked": 0, "uncheckedKeysByFile": [], "unmatched": 0, "updated": 0, } `) expect(fs.readFileSync(testFile, 'utf-8')).toContain( 'expect(value).toMatchInlineSnapshot(`"test1"`)', )
// update run vitest = await runVitest({ root, include: [testFile], update: true, }) expect(vitest.ctx?.snapshot.summary).toMatchInlineSnapshot(` { "added": 0, "didUpdate": true, "failure": false, "filesAdded": 0, "filesRemoved": 0, "filesRemovedList": [], "filesUnmatched": 0, "filesUpdated": 0, "matched": 2, "total": 2, "unchecked": 0, "uncheckedKeysByFile": [], "unmatched": 0, "updated": 0, } `) expect(fs.readFileSync(testFile, 'utf-8')).toContain( 'expect(value).toMatchInlineSnapshot(`"test1"`)', )})
test('same snapshots in multiple tests', async () => { // pnpm -C test/snapshots test:fixtures --root test/fixtures/inline-multiple-calls same2.test
// reset snapshot const root = join(import.meta.dirname, 'fixtures/inline-multiple-calls') const testFile = join(root, 'same2.test.ts') editFile(testFile, (s) => s.replace(/toMatchInlineSnapshot\(`.*`\)/gs, 'toMatchInlineSnapshot()'))
// initial run (create snapshot) let vitest = await runVitest({ root, include: [testFile], update: true, }) expect(vitest.stderr).toBe('') expect(vitest.ctx?.snapshot.summary).toMatchInlineSnapshot(` { "added": 2, "didUpdate": true, "failure": false, "filesAdded": 1, "filesRemoved": 0, "filesRemovedList": [], "filesUnmatched": 0, "filesUpdated": 0, "matched": 0, "total": 2, "unchecked": 0, "uncheckedKeysByFile": [], "unmatched": 0, "updated": 0, } `) expect(fs.readFileSync(testFile, 'utf-8')).toContain( 'expect(value).toMatchInlineSnapshot(`"test1"`)', )
// no-update run vitest = await runVitest({ root, include: [testFile], update: false, }) expect(vitest.stderr).toBe('') expect(vitest.ctx?.snapshot.summary).toMatchInlineSnapshot(` { "added": 0, "didUpdate": false, "failure": false, "filesAdded": 0, "filesRemoved": 0, "filesRemovedList": [], "filesUnmatched": 0, "filesUpdated": 0, "matched": 2, "total": 2, "unchecked": 0, "uncheckedKeysByFile": [], "unmatched": 0, "updated": 0, } `) expect(fs.readFileSync(testFile, 'utf-8')).toContain( 'expect(value).toMatchInlineSnapshot(`"test1"`)', )
// update run vitest = await runVitest({ root, include: [testFile], update: true, }) expect(vitest.ctx?.snapshot.summary).toMatchInlineSnapshot(` { "added": 0, "didUpdate": true, "failure": false, "filesAdded": 0, "filesRemoved": 0, "filesRemovedList": [], "filesUnmatched": 0, "filesUpdated": 0, "matched": 2, "total": 2, "unchecked": 0, "uncheckedKeysByFile": [], "unmatched": 0, "updated": 0, } `) expect(fs.readFileSync(testFile, 'utf-8')).toContain( 'expect(value).toMatchInlineSnapshot(`"test1"`)', )})
test('different snapshots in single test', async () => { // pnpm -C test/snapshots test:fixtures --root test/fixtures/inline-multiple-calls different.test
// reset snapshot const root = join(import.meta.dirname, 'fixtures/inline-multiple-calls') const testFile = join(root, 'different.test.ts') editFile(testFile, (s) => s.replace(/toMatchInlineSnapshot\(`.*`\)/gs, 'toMatchInlineSnapshot()'))
// update run should fail let vitest = await runVitest({ root, include: [testFile], update: true, }) expect(vitest.stderr).toContain(`Error: toMatchInlineSnapshot with different snapshots cannot be called at the same location
Expected: ""test1""Received: ""test2""`) expect(fs.readFileSync(testFile, 'utf-8')).toContain('expect(value).toMatchInlineSnapshot()') expect(vitest.exitCode).not.toBe(0)
// no-update run should fail vitest = await runVitest({ root, include: [testFile], update: false, }) if (process.env.CI) { expect(vitest.stderr).toContain(`Error: Snapshot \`single 1\` mismatched`) } else { expect(vitest.stderr).toContain(`Error: toMatchInlineSnapshot with different snapshots cannot be called at the same location
Expected: ""test1""Received: ""test2""`) } expect(fs.readFileSync(testFile, 'utf-8')).toContain('expect(value).toMatchInlineSnapshot()')
// current snapshot is "test1" editFile(testFile, (s) => s.replace( 'expect(value).toMatchInlineSnapshot()', 'expect(value).toMatchInlineSnapshot(`"test1"`)', ), ) vitest = await runVitest({ root, include: [testFile], update: true, }) expect(vitest.stderr).toContain(`Error: toMatchInlineSnapshot with different snapshots cannot be called at the same location
Expected: ""test1""Received: ""test2""`) expect(fs.readFileSync(testFile, 'utf-8')).toContain( 'expect(value).toMatchInlineSnapshot(`"test1"`)', )
vitest = await runVitest({ root, include: [testFile], update: false, }) expect(vitest.stderr).toContain(`Error: toMatchInlineSnapshot with different snapshots cannot be called at the same location
Expected: ""test1""Received: ""test2""`) expect(fs.readFileSync(testFile, 'utf-8')).toContain( 'expect(value).toMatchInlineSnapshot(`"test1"`)', )
// current snapshot is "test2" editFile(testFile, (s) => s.replace( 'expect(value).toMatchInlineSnapshot(`"test1"`)', 'expect(value).toMatchInlineSnapshot(`"test2"`)', ), ) vitest = await runVitest({ root, include: [testFile], update: true, }) expect(vitest.stderr).toContain(`Error: toMatchInlineSnapshot with different snapshots cannot be called at the same location
Expected: ""test1""Received: ""test2""`) expect(fs.readFileSync(testFile, 'utf-8')).toContain( 'expect(value).toMatchInlineSnapshot(`"test2"`)', )
vitest = await runVitest({ root, include: [testFile], update: false, }) expect(vitest.stderr).toContain(`Error: Snapshot \`single 1\` mismatched
Expected: ""test2""Received: ""test1""`) expect(fs.readFileSync(testFile, 'utf-8')).toContain( 'expect(value).toMatchInlineSnapshot(`"test2"`)', )})
test('different snapshots in multiple tests', async () => { // pnpm -C test/snapshots test:fixtures --root test/fixtures/inline-multiple-calls different2.test
// reset snapshot const root = join(import.meta.dirname, 'fixtures/inline-multiple-calls') const testFile = join(root, 'different2.test.ts') editFile(testFile, (s) => s.replace(/toMatchInlineSnapshot\(`.*`\)/gs, 'toMatchInlineSnapshot()'))
// update run should fail let vitest = await runVitest({ root, include: [testFile], update: true, }) expect(vitest.stderr).toContain(`Error: toMatchInlineSnapshot with different snapshots cannot be called at the same location
Expected: ""test1""Received: ""test2""`) expect(fs.readFileSync(testFile, 'utf-8')).toContain('expect(value).toMatchInlineSnapshot()') expect(vitest.exitCode).not.toBe(0)
// no-update run should fail vitest = await runVitest({ root, include: [testFile], update: false, }) if (process.env.CI) { expect(vitest.stderr).toContain(`Error: Snapshot \`a 1\` mismatched`) } else { expect(vitest.stderr).toContain(`Error: toMatchInlineSnapshot with different snapshots cannot be called at the same location
Expected: ""test1""Received: ""test2""`) } expect(fs.readFileSync(testFile, 'utf-8')).toContain('expect(value).toMatchInlineSnapshot()')
// current snapshot is "test1" editFile(testFile, (s) => s.replace( 'expect(value).toMatchInlineSnapshot()', 'expect(value).toMatchInlineSnapshot(`"test1"`)', ), ) vitest = await runVitest({ root, include: [testFile], update: true, }) expect(vitest.stderr).toContain(`Error: toMatchInlineSnapshot with different snapshots cannot be called at the same location
Expected: ""test1""Received: ""test2""`) expect(fs.readFileSync(testFile, 'utf-8')).toContain( 'expect(value).toMatchInlineSnapshot(`"test1"`)', )
vitest = await runVitest({ root, include: [testFile], update: false, }) expect(vitest.stderr).toContain(`Error: toMatchInlineSnapshot with different snapshots cannot be called at the same location
Expected: ""test1""Received: ""test2""`) expect(fs.readFileSync(testFile, 'utf-8')).toContain( 'expect(value).toMatchInlineSnapshot(`"test1"`)', )
// current snapshot is "test2" editFile(testFile, (s) => s.replace( 'expect(value).toMatchInlineSnapshot(`"test1"`)', 'expect(value).toMatchInlineSnapshot(`"test2"`)', ), ) vitest = await runVitest({ root, include: [testFile], update: true, }) expect(vitest.stderr).toContain(`Error: toMatchInlineSnapshot with different snapshots cannot be called at the same location
Expected: ""test1""Received: ""test2""`) expect(fs.readFileSync(testFile, 'utf-8')).toContain( 'expect(value).toMatchInlineSnapshot(`"test2"`)', )
vitest = await runVitest({ root, include: [testFile], update: false, }) expect(vitest.stderr).toContain(`Error: Snapshot \`a 1\` mismatched
Expected: ""test2""Received: ""test1""`) expect(fs.readFileSync(testFile, 'utf-8')).toContain( 'expect(value).toMatchInlineSnapshot(`"test2"`)', )})
test('test.each/for', async () => { const root = join(import.meta.dirname, 'fixtures/inline-multiple-calls') const testFile = join(root, 'each.test.ts')
// remove inline snapshots editFile(testFile, (s) => s .replace(/toMatchInlineSnapshot\(`[^`]*`\)/g, 'toMatchInlineSnapshot()') .replace( /toThrowErrorMatchingInlineSnapshot\(`[^`]*`\)/g, 'toThrowErrorMatchingInlineSnapshot()', ), )
// create snapshots from scratch let result = await runVitest({ root, include: [testFile], update: 'new' }) expect(result.stderr).toMatchInlineSnapshot(`""`) expect(readFileSync(testFile, 'utf-8')).toMatchInlineSnapshot(` "import { expect, test, describe } from "vitest";
test.for(["hello", "world"])("test %s", (arg) => { expect(arg.length).toMatchInlineSnapshot(\`5\`); });
describe.for(["hello", "world"])("suite %s", (arg) => { test("length", () => { expect(arg.length).toMatchInlineSnapshot(\`5\`); }); });
test.for(["hello", "world"])("toThrowErrorMatchingInlineSnapshot %s", (arg) => { expect(() => { throw new Error(\`length = \${arg.length}\`); }).toThrowErrorMatchingInlineSnapshot(\`[Error: length = 5]\`) }); " `) expect(result.errorTree()).toMatchInlineSnapshot(` { "each.test.ts": { "suite hello": { "length": "passed", }, "suite world": { "length": "passed", }, "test hello": "passed", "test world": "passed", "toThrowErrorMatchingInlineSnapshot hello": "passed", "toThrowErrorMatchingInlineSnapshot world": "passed", }, } `) expect(result.ctx?.snapshot.summary).toMatchInlineSnapshot(` { "added": 6, "didUpdate": false, "failure": false, "filesAdded": 1, "filesRemoved": 0, "filesRemovedList": [], "filesUnmatched": 0, "filesUpdated": 0, "matched": 0, "total": 6, "unchecked": 0, "uncheckedKeysByFile": [], "unmatched": 0, "updated": 0, } `)
// edit tests to introduce errors editFile(testFile, (s) => s.replaceAll(`"hello"`, `"hey"`))
// fails with update=false result = await runVitest({ root, include: [testFile], update: false }) expect(result.stderr).toMatchInlineSnapshot(` " ⎯⎯⎯⎯⎯⎯⎯ Failed Tests 6 ⎯⎯⎯⎯⎯⎯⎯
FAIL each.test.ts > test hey Error: Snapshot \`test hey 1\` mismatched
Expected: "5" Received: "3"
❯ each.test.ts:4:22 2| 3| test.for(["hey", "world"])("test %s", (arg) => { 4| expect(arg.length).toMatchInlineSnapshot(\`5\`); | ^ 5| }); 6|
⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯[1/6]⎯
FAIL each.test.ts > test world Error: toMatchInlineSnapshot with different snapshots cannot be called at the same location
Expected: "3" Received: "5"
❯ each.test.ts:4:22 2| 3| test.for(["hey", "world"])("test %s", (arg) => { 4| expect(arg.length).toMatchInlineSnapshot(\`5\`); | ^ 5| }); 6|
⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯[2/6]⎯
FAIL each.test.ts > suite hey > length Error: Snapshot \`suite hey > length 1\` mismatched
Expected: "5" Received: "3"
❯ each.test.ts:9:24 7| describe.for(["hey", "world"])("suite %s", (arg) => { 8| test("length", () => { 9| expect(arg.length).toMatchInlineSnapshot(\`5\`); | ^ 10| }); 11| });
⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯[3/6]⎯
FAIL each.test.ts > suite world > length Error: toMatchInlineSnapshot with different snapshots cannot be called at the same location
Expected: "3" Received: "5"
❯ each.test.ts:9:24 7| describe.for(["hey", "world"])("suite %s", (arg) => { 8| test("length", () => { 9| expect(arg.length).toMatchInlineSnapshot(\`5\`); | ^ 10| }); 11| });
⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯[4/6]⎯
FAIL each.test.ts > toThrowErrorMatchingInlineSnapshot hey Error: Snapshot \`toThrowErrorMatchingInlineSnapshot hey 1\` mismatched
Expected: "[Error: length = 5]" Received: "[Error: length = 3]"
❯ each.test.ts:16:6 14| expect(() => { 15| throw new Error(\`length = \${arg.length}\`); 16| }).toThrowErrorMatchingInlineSnapshot(\`[Error: length = 5]\`) | ^ 17| }); 18|
⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯[5/6]⎯
FAIL each.test.ts > toThrowErrorMatchingInlineSnapshot world Error: toThrowErrorMatchingInlineSnapshot with different snapshots cannot be called at the same location
Expected: "[Error: length = 3]" Received: "[Error: length = 5]"
❯ each.test.ts:16:6 14| expect(() => { 15| throw new Error(\`length = \${arg.length}\`); 16| }).toThrowErrorMatchingInlineSnapshot(\`[Error: length = 5]\`) | ^ 17| }); 18|
⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯[6/6]⎯
" `) expect(result.errorTree()).toMatchInlineSnapshot(` { "each.test.ts": { "suite hey": { "length": [ "Snapshot \`suite hey > length 1\` mismatched", ], }, "suite world": { "length": [ "toMatchInlineSnapshot with different snapshots cannot be called at the same location", ], }, "test hey": [ "Snapshot \`test hey 1\` mismatched", ], "test world": [ "toMatchInlineSnapshot with different snapshots cannot be called at the same location", ], "toThrowErrorMatchingInlineSnapshot hey": [ "Snapshot \`toThrowErrorMatchingInlineSnapshot hey 1\` mismatched", ], "toThrowErrorMatchingInlineSnapshot world": [ "toThrowErrorMatchingInlineSnapshot with different snapshots cannot be called at the same location", ], }, } `) expect(result.ctx?.snapshot.summary).toMatchInlineSnapshot(` { "added": 0, "didUpdate": false, "failure": false, "filesAdded": 0, "filesRemoved": 0, "filesRemovedList": [], "filesUnmatched": 1, "filesUpdated": 0, "matched": 0, "total": 3, "unchecked": 0, "uncheckedKeysByFile": [], "unmatched": 3, "updated": 0, } `)
// fails with update=all result = await runVitest({ root, include: [testFile], update: 'all' }) expect(result.stderr).toMatchInlineSnapshot(` " ⎯⎯⎯⎯⎯⎯⎯ Failed Tests 3 ⎯⎯⎯⎯⎯⎯⎯
FAIL each.test.ts > test world Error: toMatchInlineSnapshot with different snapshots cannot be called at the same location
Expected: "3" Received: "5"
❯ each.test.ts:4:22 2| 3| test.for(["hey", "world"])("test %s", (arg) => { 4| expect(arg.length).toMatchInlineSnapshot(\`5\`); | ^ 5| }); 6|
⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯[1/3]⎯
FAIL each.test.ts > suite world > length Error: toMatchInlineSnapshot with different snapshots cannot be called at the same location
Expected: "3" Received: "5"
❯ each.test.ts:9:24 7| describe.for(["hey", "world"])("suite %s", (arg) => { 8| test("length", () => { 9| expect(arg.length).toMatchInlineSnapshot(\`5\`); | ^ 10| }); 11| });
⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯[2/3]⎯
FAIL each.test.ts > toThrowErrorMatchingInlineSnapshot world Error: toThrowErrorMatchingInlineSnapshot with different snapshots cannot be called at the same location
Expected: "[Error: length = 3]" Received: "[Error: length = 5]"
❯ each.test.ts:16:6 14| expect(() => { 15| throw new Error(\`length = \${arg.length}\`); 16| }).toThrowErrorMatchingInlineSnapshot(\`[Error: length = 5]\`) | ^ 17| }); 18|
⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯[3/3]⎯
" `) expect(result.errorTree()).toMatchInlineSnapshot(` { "each.test.ts": { "suite hey": { "length": "passed", }, "suite world": { "length": [ "toMatchInlineSnapshot with different snapshots cannot be called at the same location", ], }, "test hey": "passed", "test world": [ "toMatchInlineSnapshot with different snapshots cannot be called at the same location", ], "toThrowErrorMatchingInlineSnapshot hey": "passed", "toThrowErrorMatchingInlineSnapshot world": [ "toThrowErrorMatchingInlineSnapshot with different snapshots cannot be called at the same location", ], }, } `) expect(readFileSync(testFile, 'utf-8')).toMatchInlineSnapshot(` "import { expect, test, describe } from "vitest";
test.for(["hey", "world"])("test %s", (arg) => { expect(arg.length).toMatchInlineSnapshot(\`5\`); });
describe.for(["hey", "world"])("suite %s", (arg) => { test("length", () => { expect(arg.length).toMatchInlineSnapshot(\`5\`); }); });
test.for(["hey", "world"])("toThrowErrorMatchingInlineSnapshot %s", (arg) => { expect(() => { throw new Error(\`length = \${arg.length}\`); }).toThrowErrorMatchingInlineSnapshot(\`[Error: length = 5]\`) }); " `) expect(result.ctx?.snapshot.summary).toMatchInlineSnapshot(` { "added": 0, "didUpdate": true, "failure": false, "filesAdded": 0, "filesRemoved": 0, "filesRemovedList": [], "filesUnmatched": 0, "filesUpdated": 1, "matched": 0, "total": 3, "unchecked": 0, "uncheckedKeysByFile": [], "unmatched": 0, "updated": 3, } `)})