[READ-ONLY] Mirror of https://github.com/vitest-dev/vitest. Next generation testing framework powered by Vite. vitest.dev
test testing-tools vite
12

Configure Feed

Select the types of activity you want to include in your feed.

fix(snapshot): reject multiple `toMatchInlineSnapshot` updates at the same location (#6332)

authored by

Hiroshi Ogawa and committed by
GitHub
(Aug 13, 2024, 12:59 PM +0200) 1606f34f 153ff01b

+50 -1
+4 -1
test/cli/test/fails.test.ts
··· 8 8 const files = await fg('**/*.test.ts', { cwd: root, dot: true }) 9 9 10 10 it.each(files)('should fail %s', async (file) => { 11 - const { stderr } = await runVitest({ root }, [file]) 11 + const { stderr } = await runVitest({ 12 + root, 13 + update: file === 'inline-snapshop-inside-loop-update-all.test.ts' ? true : undefined, 14 + }, [file]) 12 15 13 16 expect(stderr).toBeTruthy() 14 17 const msg = String(stderr)
+7
packages/snapshot/src/port/state.ts
··· 158 158 // location for js files, but `column-1` points to the same in both js/ts 159 159 // https://github.com/vitejs/vite/issues/8657 160 160 stack.column-- 161 + // reject multiple inline snapshots at the same location 162 + const duplicateIndex = this._inlineSnapshots.findIndex(s => s.file === stack.file && s.line === stack.line && s.column === stack.column) 163 + if (duplicateIndex >= 0) { 164 + // remove the first one to avoid updating an inline snapshot 165 + this._inlineSnapshots.splice(duplicateIndex, 1) 166 + throw new Error('toMatchInlineSnapshot cannot be called multiple times at the same location.') 167 + } 161 168 this._inlineSnapshots.push({ 162 169 snapshot: receivedSerialized, 163 170 ...stack,
+23
test/cli/fixtures/fails/inline-snapshop-inside-loop-update-all.test.ts
··· 1 + import {test, expect} from "vitest"; 2 + 3 + test("ok", () => { 4 + expect("ok").toMatchInlineSnapshot(`"ok"`); 5 + }); 6 + 7 + test("fail 1", () => { 8 + for (const str of ["foo", "bar"]) { 9 + expect(str).toMatchInlineSnapshot(); 10 + } 11 + }); 12 + 13 + test("fail 3", () => { 14 + for (const str of ["ok", "ok"]) { 15 + expect(str).toMatchInlineSnapshot(); 16 + } 17 + }); 18 + 19 + test("somehow ok", () => { 20 + for (const str of ["ok", "ok"]) { 21 + expect(str).toMatchInlineSnapshot(`"ok"`); 22 + } 23 + });
+9
test/cli/fixtures/fails/inline-snapshop-inside-loop-update-none.test.ts
··· 1 + import {test, expect} from "vitest"; 2 + 3 + // this test causes infinite re-run when --watch and --update 4 + // since snapshot update switches between "foo" and "bar" forever. 5 + test("fail 2", () => { 6 + for (const str of ["foo", "bar"]) { 7 + expect(str).toMatchInlineSnapshot(`"bar"`); 8 + } 9 + });
+7
test/cli/test/__snapshots__/fails.test.ts.snap
··· 31 31 Error: InlineSnapshot cannot be used inside of test.each or describe.each" 32 32 `; 33 33 34 + exports[`should fail inline-snapshop-inside-loop-update-all.test.ts > inline-snapshop-inside-loop-update-all.test.ts 1`] = ` 35 + "Error: toMatchInlineSnapshot cannot be called multiple times at the same location. 36 + Error: toMatchInlineSnapshot cannot be called multiple times at the same location." 37 + `; 38 + 39 + exports[`should fail inline-snapshop-inside-loop-update-none.test.ts > inline-snapshop-inside-loop-update-none.test.ts 1`] = `"Error: Snapshot \`fail 2 1\` mismatched"`; 40 + 34 41 exports[`should fail mock-import-proxy-module.test.ts > mock-import-proxy-module.test.ts 1`] = `"Error: There are some problems in resolving the mocks API."`; 35 42 36 43 exports[`should fail nested-suite.test.ts > nested-suite.test.ts 1`] = `"AssertionError: expected true to be false // Object.is equality"`;