From a7bf85911cd7f307b87aed530648b19537e25208 Mon Sep 17 00:00:00 2001 From: jycouet Date: Tue, 11 Feb 2025 21:47:46 +0100 Subject: [PATCH 01/24] fix(inline-snapshot): can be used by multiple tests. On clean state, will take the first result. --- packages/snapshot/src/port/state.ts | 19 ++++++++----------- .../fails/inline-snapshop-inside-loop.test.ts | 18 +++--------------- .../cli/test/__snapshots__/fails.test.ts.snap | 8 -------- 3 files changed, 11 insertions(+), 34 deletions(-) diff --git a/packages/snapshot/src/port/state.ts b/packages/snapshot/src/port/state.ts index be480185ad11..9fa669b869c9 100644 --- a/packages/snapshot/src/port/state.ts +++ b/packages/snapshot/src/port/state.ts @@ -178,11 +178,14 @@ export default class SnapshotState { ): void { this._dirty = true if (options.stack) { - this._inlineSnapshots.push({ - snapshot: receivedSerialized, - testId: options.testId, - ...options.stack, - }) + // In a clean state, we don't want to set the inlineSnapshots 2 times with the same testId + if(this._inlineSnapshots.findIndex(s => s.testId === options.testId) === -1) { + this._inlineSnapshots.push({ + snapshot: receivedSerialized, + testId: options.testId, + ...options.stack, + }) + } } else if (options.rawSnapshot) { this._rawSnapshots.push({ @@ -343,12 +346,6 @@ export default class SnapshotState { // https://github.com/vitejs/vite/issues/8657 stack.column-- - // reject multiple inline snapshots at the same location - if (this._inlineSnapshotStacks.some(s => s.file === stack!.file && s.line === stack!.line && s.column === stack!.column)) { - // remove already succeeded snapshot - this._inlineSnapshots = this._inlineSnapshots.filter(s => !(s.file === stack!.file && s.line === stack!.line && s.column === stack!.column)) - throw new Error('toMatchInlineSnapshot cannot be called multiple times at the same location.') - } this._inlineSnapshotStacks.push({ ...stack, testId }) } diff --git a/test/cli/fixtures/fails/inline-snapshop-inside-loop.test.ts b/test/cli/fixtures/fails/inline-snapshop-inside-loop.test.ts index a03c6aa5aaf9..1d09648de17f 100644 --- a/test/cli/fixtures/fails/inline-snapshop-inside-loop.test.ts +++ b/test/cli/fixtures/fails/inline-snapshop-inside-loop.test.ts @@ -2,30 +2,18 @@ import {test, expect} from "vitest"; test("fail 1", () => { for (const str of ["foo", "bar"]) { - expect(str).toMatchInlineSnapshot(); + expect(str).toMatchInlineSnapshot(`"foo"`); } }); test("fail 2.1", () => { for (const str of ["foo", "bar"]) { - expect(str).toMatchInlineSnapshot(`"foo"`); + expect(str).toMatchInlineSnapshot(`"bar"`); } }); test("fail 2.2", () => { for (const str of ["foo", "bar"]) { - expect(str).toMatchInlineSnapshot(`"bar"`); - } -}); - -test("fail 3", () => { - for (const str of ["ok", "ok"]) { - expect(str).toMatchInlineSnapshot(); - } -}); - -test("fail 4", () => { - for (const str of ["ok", "ok"]) { - expect(str).toMatchInlineSnapshot(`"ok"`); + expect(str).toMatchInlineSnapshot(`"foo"`); } }); diff --git a/test/cli/test/__snapshots__/fails.test.ts.snap b/test/cli/test/__snapshots__/fails.test.ts.snap index 38c0ef3e2389..e75221b82057 100644 --- a/test/cli/test/__snapshots__/fails.test.ts.snap +++ b/test/cli/test/__snapshots__/fails.test.ts.snap @@ -50,14 +50,6 @@ Error: InlineSnapshot cannot be used inside of test.each or describe.each Error: InlineSnapshot cannot be used inside of test.each or describe.each" `; -exports[`should fail inline-snapshop-inside-loop.test.ts 1`] = ` -"Error: toMatchInlineSnapshot cannot be called multiple times at the same location. -Error: toMatchInlineSnapshot cannot be called multiple times at the same location. -Error: toMatchInlineSnapshot cannot be called multiple times at the same location. -Error: toMatchInlineSnapshot cannot be called multiple times at the same location. -Error: toMatchInlineSnapshot cannot be called multiple times at the same location." -`; - exports[`should fail mock-import-proxy-module.test.ts 1`] = `"Error: There are some problems in resolving the mocks API."`; exports[`should fail nested-suite.test.ts 1`] = `"AssertionError: expected true to be false // Object.is equality"`; From ffc1fd2eabe6c9ac3305272242acb6430258eb27 Mon Sep 17 00:00:00 2001 From: jycouet Date: Tue, 11 Feb 2025 22:26:57 +0100 Subject: [PATCH 02/24] linting --- packages/snapshot/src/port/state.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/snapshot/src/port/state.ts b/packages/snapshot/src/port/state.ts index 9fa669b869c9..cb660469170f 100644 --- a/packages/snapshot/src/port/state.ts +++ b/packages/snapshot/src/port/state.ts @@ -179,7 +179,7 @@ export default class SnapshotState { this._dirty = true if (options.stack) { // In a clean state, we don't want to set the inlineSnapshots 2 times with the same testId - if(this._inlineSnapshots.findIndex(s => s.testId === options.testId) === -1) { + if (this._inlineSnapshots.findIndex(s => s.testId === options.testId) === -1) { this._inlineSnapshots.push({ snapshot: receivedSerialized, testId: options.testId, From ebe1a067cd6a8ce5270d2f51adf5d5c32558dbe1 Mon Sep 17 00:00:00 2001 From: jycouet Date: Tue, 11 Feb 2025 22:37:09 +0100 Subject: [PATCH 03/24] Keep 1 failing test --- .../fails/inline-snapshop-inside-loop.test.ts | 14 +------------- 1 file changed, 1 insertion(+), 13 deletions(-) diff --git a/test/cli/fixtures/fails/inline-snapshop-inside-loop.test.ts b/test/cli/fixtures/fails/inline-snapshop-inside-loop.test.ts index 1d09648de17f..7dad57e0fe24 100644 --- a/test/cli/fixtures/fails/inline-snapshop-inside-loop.test.ts +++ b/test/cli/fixtures/fails/inline-snapshop-inside-loop.test.ts @@ -4,16 +4,4 @@ test("fail 1", () => { for (const str of ["foo", "bar"]) { expect(str).toMatchInlineSnapshot(`"foo"`); } -}); - -test("fail 2.1", () => { - for (const str of ["foo", "bar"]) { - expect(str).toMatchInlineSnapshot(`"bar"`); - } -}); - -test("fail 2.2", () => { - for (const str of ["foo", "bar"]) { - expect(str).toMatchInlineSnapshot(`"foo"`); - } -}); +}); \ No newline at end of file From 33274aea7b62c851d5eb096a484e587d603ad486 Mon Sep 17 00:00:00 2001 From: jycouet Date: Tue, 11 Feb 2025 22:45:58 +0100 Subject: [PATCH 04/24] test the initial case --- test/cli/fixtures/fails/inline-snapshop-inside-loop.test.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/cli/fixtures/fails/inline-snapshop-inside-loop.test.ts b/test/cli/fixtures/fails/inline-snapshop-inside-loop.test.ts index 7dad57e0fe24..e41fd6ca6b3d 100644 --- a/test/cli/fixtures/fails/inline-snapshop-inside-loop.test.ts +++ b/test/cli/fixtures/fails/inline-snapshop-inside-loop.test.ts @@ -2,6 +2,6 @@ import {test, expect} from "vitest"; test("fail 1", () => { for (const str of ["foo", "bar"]) { - expect(str).toMatchInlineSnapshot(`"foo"`); + expect(str).toMatchInlineSnapshot(); } }); \ No newline at end of file From 1c0f9fcad8f20c050fda12ea7cf1039a43340035 Mon Sep 17 00:00:00 2001 From: jycouet Date: Tue, 11 Feb 2025 23:02:48 +0100 Subject: [PATCH 05/24] should be better :) --- test/cli/fixtures/fails/inline-snapshop-inside-loop.test.ts | 2 +- test/cli/test/__snapshots__/fails.test.ts.snap | 2 ++ test/cli/test/fails.test.ts | 5 +---- 3 files changed, 4 insertions(+), 5 deletions(-) diff --git a/test/cli/fixtures/fails/inline-snapshop-inside-loop.test.ts b/test/cli/fixtures/fails/inline-snapshop-inside-loop.test.ts index e41fd6ca6b3d..7dad57e0fe24 100644 --- a/test/cli/fixtures/fails/inline-snapshop-inside-loop.test.ts +++ b/test/cli/fixtures/fails/inline-snapshop-inside-loop.test.ts @@ -2,6 +2,6 @@ import {test, expect} from "vitest"; test("fail 1", () => { for (const str of ["foo", "bar"]) { - expect(str).toMatchInlineSnapshot(); + expect(str).toMatchInlineSnapshot(`"foo"`); } }); \ No newline at end of file diff --git a/test/cli/test/__snapshots__/fails.test.ts.snap b/test/cli/test/__snapshots__/fails.test.ts.snap index e75221b82057..4fc59498570f 100644 --- a/test/cli/test/__snapshots__/fails.test.ts.snap +++ b/test/cli/test/__snapshots__/fails.test.ts.snap @@ -50,6 +50,8 @@ Error: InlineSnapshot cannot be used inside of test.each or describe.each Error: InlineSnapshot cannot be used inside of test.each or describe.each" `; +exports[`should fail inline-snapshop-inside-loop.test.ts 1`] = `"Error: Snapshot \`fail 1 2\` mismatched"`; + exports[`should fail mock-import-proxy-module.test.ts 1`] = `"Error: There are some problems in resolving the mocks API."`; exports[`should fail nested-suite.test.ts 1`] = `"AssertionError: expected true to be false // Object.is equality"`; diff --git a/test/cli/test/fails.test.ts b/test/cli/test/fails.test.ts index 406d5d974426..9b719f76796f 100644 --- a/test/cli/test/fails.test.ts +++ b/test/cli/test/fails.test.ts @@ -9,10 +9,7 @@ const root = resolve(__dirname, '../fixtures/fails') const files = await glob(['**/*.test.ts'], { cwd: root, dot: true, expandDirectories: false }) it.each(files)('should fail %s', async (file) => { - const { stderr } = await runVitest({ - root, - update: file === 'inline-snapshop-inside-loop.test.ts' ? true : undefined, - }, [file]) + const { stderr } = await runVitest({ root }, [file]) expect(stderr).toBeTruthy() const msg = String(stderr) From a78ba5249b47197d5130b65af6c5ec39132809b5 Mon Sep 17 00:00:00 2001 From: jycouet Date: Tue, 11 Feb 2025 23:37:44 +0100 Subject: [PATCH 06/24] step by step understanding how it works --- packages/snapshot/src/port/state.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/snapshot/src/port/state.ts b/packages/snapshot/src/port/state.ts index cb660469170f..2562b7517cee 100644 --- a/packages/snapshot/src/port/state.ts +++ b/packages/snapshot/src/port/state.ts @@ -178,8 +178,8 @@ export default class SnapshotState { ): void { this._dirty = true if (options.stack) { - // In a clean state, we don't want to set the inlineSnapshots 2 times with the same testId - if (this._inlineSnapshots.findIndex(s => s.testId === options.testId) === -1) { + // In a clean state, we don't want to set the inlineSnapshots 2 times + if (this._inlineSnapshotStacks.some(s => s.file === options.stack!.file && s.line === options.stack!.line && s.column === options.stack!.column)) { this._inlineSnapshots.push({ snapshot: receivedSerialized, testId: options.testId, From 4ec405b42b7b9fcf4da832a52a73d159ea9aaeec Mon Sep 17 00:00:00 2001 From: jycouet Date: Wed, 12 Feb 2025 11:09:14 +0100 Subject: [PATCH 07/24] Adding tests and fixing code --- packages/snapshot/src/port/inlineSnapshot.ts | 8 ++++- packages/snapshot/src/port/state.ts | 13 +++---- .../test-update/inline-reuse.test.js | 31 ++++++++++++++++ .../__snapshots__/test-update.test.ts.snap | 35 +++++++++++++++++++ .../fixtures/test-update/inline-reuse.test.js | 31 ++++++++++++++++ test/snapshots/test/reuse.test.ts | 21 +++++++++++ 6 files changed, 130 insertions(+), 9 deletions(-) create mode 100644 test/snapshots/test-update/inline-reuse.test.js create mode 100644 test/snapshots/test/fixtures/test-update/inline-reuse.test.js create mode 100644 test/snapshots/test/reuse.test.ts diff --git a/packages/snapshot/src/port/inlineSnapshot.ts b/packages/snapshot/src/port/inlineSnapshot.ts index 09f059661655..ad86e80fec42 100644 --- a/packages/snapshot/src/port/inlineSnapshot.ts +++ b/packages/snapshot/src/port/inlineSnapshot.ts @@ -27,7 +27,13 @@ export async function saveInlineSnapshots( const code = await environment.readSnapshotFile(file) as string const s = new MagicString(code) - for (const snap of snaps) { + const uniqueSnaps = snaps.reduce((acc: InlineSnapshot[], snap) => { + const exists = acc.some(s => s.line === snap.line && s.column === snap.column) + if (!exists) acc.push(snap) + return acc + }, []) + + for (const snap of uniqueSnaps) { const index = positionToOffset(code, snap.line, snap.column) replaceInlineSnap(code, s, index, snap.snapshot) } diff --git a/packages/snapshot/src/port/state.ts b/packages/snapshot/src/port/state.ts index 2562b7517cee..b3b12ea2a673 100644 --- a/packages/snapshot/src/port/state.ts +++ b/packages/snapshot/src/port/state.ts @@ -178,14 +178,11 @@ export default class SnapshotState { ): void { this._dirty = true if (options.stack) { - // In a clean state, we don't want to set the inlineSnapshots 2 times - if (this._inlineSnapshotStacks.some(s => s.file === options.stack!.file && s.line === options.stack!.line && s.column === options.stack!.column)) { - this._inlineSnapshots.push({ - snapshot: receivedSerialized, - testId: options.testId, - ...options.stack, - }) - } + this._inlineSnapshots.push({ + snapshot: receivedSerialized, + testId: options.testId, + ...options.stack, + }) } else if (options.rawSnapshot) { this._rawSnapshots.push({ diff --git a/test/snapshots/test-update/inline-reuse.test.js b/test/snapshots/test-update/inline-reuse.test.js new file mode 100644 index 000000000000..64dfef5096d6 --- /dev/null +++ b/test/snapshots/test-update/inline-reuse.test.js @@ -0,0 +1,31 @@ +import { expect, test } from "vitest"; + +// when snapshots are generated Vitest reruns `toMatchInlineSnapshot` checks +// please, don't commit generated snapshots +test('a', () => { + myTest1(); + myTest2(); + myTest3(); +}); + +test('b', () => { + myTest1(); + myTest2(); + myTest3(); +}); + + +function myTest1() { + expect(7 + 7).toMatchInlineSnapshot(`14`); +} + +function myTest2() { + expect(3 + 3).toMatchInlineSnapshot(`6`); + expect(4 + 4).toMatchInlineSnapshot(`8`); +} + +function myTest3() { + expect(5 + 5).toMatchInlineSnapshot(`10`); + expect(5 + 6).toMatchInlineSnapshot(`11`); + expect(6 + 6).toMatchInlineSnapshot(`12`); +} diff --git a/test/snapshots/test/__snapshots__/test-update.test.ts.snap b/test/snapshots/test/__snapshots__/test-update.test.ts.snap index 3321ee7ef68e..c4efa048c4d5 100644 --- a/test/snapshots/test/__snapshots__/test-update.test.ts.snap +++ b/test/snapshots/test/__snapshots__/test-update.test.ts.snap @@ -90,6 +90,41 @@ it.concurrent('3rd', ({ expect }) => { " `; +exports[`inline-reuse.test.js 1`] = ` +"import { expect, test } from "vitest"; + +// when snapshots are generated Vitest reruns \`toMatchInlineSnapshot\` checks +// please, don't commit generated snapshots +test('a', () => { + myTest1(); + myTest2(); + myTest3(); +}); + +test('b', () => { + myTest1(); + myTest2(); + myTest3(); +}); + + +function myTest1() { + expect(7 + 7).toMatchInlineSnapshot(\`14\`); +} + +function myTest2() { + expect(3 + 3).toMatchInlineSnapshot(\`6\`); + expect(4 + 4).toMatchInlineSnapshot(\`8\`); +} + +function myTest3() { + expect(5 + 5).toMatchInlineSnapshot(\`10\`); + expect(5 + 6).toMatchInlineSnapshot(\`11\`); + expect(6 + 6).toMatchInlineSnapshot(\`12\`); +} +" +`; + exports[`retry-inline.test.ts 1`] = ` "import { expect, test } from 'vitest' diff --git a/test/snapshots/test/fixtures/test-update/inline-reuse.test.js b/test/snapshots/test/fixtures/test-update/inline-reuse.test.js new file mode 100644 index 000000000000..805d1b830600 --- /dev/null +++ b/test/snapshots/test/fixtures/test-update/inline-reuse.test.js @@ -0,0 +1,31 @@ +import { expect, test } from "vitest"; + +// when snapshots are generated Vitest reruns `toMatchInlineSnapshot` checks +// please, don't commit generated snapshots +test('a', () => { + myTest1(); + myTest2(); + myTest3(); +}); + +test('b', () => { + myTest1(); + myTest2(); + myTest3(); +}); + + +function myTest1() { + expect(7 + 7).toMatchInlineSnapshot(); +} + +function myTest2() { + expect(3 + 3).toMatchInlineSnapshot(); + expect(4 + 4).toMatchInlineSnapshot(); +} + +function myTest3() { + expect(5 + 5).toMatchInlineSnapshot(`"10"`); + expect(5 + 6).toMatchInlineSnapshot(); + expect(6 + 6).toMatchInlineSnapshot(`"12"`); +} diff --git a/test/snapshots/test/reuse.test.ts b/test/snapshots/test/reuse.test.ts new file mode 100644 index 000000000000..9c3510b37356 --- /dev/null +++ b/test/snapshots/test/reuse.test.ts @@ -0,0 +1,21 @@ +import { expect, test } from "vitest"; + +test('a', () => { + myTest1(); + myTest2(); +}); + +test('b', () => { + myTest1(); + myTest2(); +}); + + +function myTest1() { + expect(7 + 7).toMatchInlineSnapshot(`14`); +} + +function myTest2() { + expect(3 + 3).toMatchInlineSnapshot(`6`); + expect(4 + 4).toMatchInlineSnapshot(`8`); +} \ No newline at end of file From 04b66c6d8cc263f3a16972c62e646fbca251cbc4 Mon Sep 17 00:00:00 2001 From: jycouet Date: Wed, 12 Feb 2025 11:21:44 +0100 Subject: [PATCH 08/24] lint:fix --- packages/snapshot/src/port/inlineSnapshot.ts | 4 ++- .../test-update/inline-reuse.test.js | 31 +++++++++---------- test/snapshots/test/reuse.test.ts | 23 +++++++------- 3 files changed, 29 insertions(+), 29 deletions(-) diff --git a/packages/snapshot/src/port/inlineSnapshot.ts b/packages/snapshot/src/port/inlineSnapshot.ts index ad86e80fec42..843cd35a9340 100644 --- a/packages/snapshot/src/port/inlineSnapshot.ts +++ b/packages/snapshot/src/port/inlineSnapshot.ts @@ -29,7 +29,9 @@ export async function saveInlineSnapshots( const uniqueSnaps = snaps.reduce((acc: InlineSnapshot[], snap) => { const exists = acc.some(s => s.line === snap.line && s.column === snap.column) - if (!exists) acc.push(snap) + if (!exists) { + acc.push(snap) + } return acc }, []) diff --git a/test/snapshots/test-update/inline-reuse.test.js b/test/snapshots/test-update/inline-reuse.test.js index 64dfef5096d6..ba2ed2af60f6 100644 --- a/test/snapshots/test-update/inline-reuse.test.js +++ b/test/snapshots/test-update/inline-reuse.test.js @@ -1,31 +1,30 @@ -import { expect, test } from "vitest"; +import { expect, test } from 'vitest' // when snapshots are generated Vitest reruns `toMatchInlineSnapshot` checks // please, don't commit generated snapshots test('a', () => { - myTest1(); - myTest2(); - myTest3(); -}); + myTest1() + myTest2() + myTest3() +}) test('b', () => { - myTest1(); - myTest2(); - myTest3(); -}); - + myTest1() + myTest2() + myTest3() +}) function myTest1() { - expect(7 + 7).toMatchInlineSnapshot(`14`); + expect(7 + 7).toMatchInlineSnapshot(`14`) } function myTest2() { - expect(3 + 3).toMatchInlineSnapshot(`6`); - expect(4 + 4).toMatchInlineSnapshot(`8`); + expect(3 + 3).toMatchInlineSnapshot(`6`) + expect(4 + 4).toMatchInlineSnapshot(`8`) } function myTest3() { - expect(5 + 5).toMatchInlineSnapshot(`10`); - expect(5 + 6).toMatchInlineSnapshot(`11`); - expect(6 + 6).toMatchInlineSnapshot(`12`); + expect(5 + 5).toMatchInlineSnapshot(`10`) + expect(5 + 6).toMatchInlineSnapshot(`11`) + expect(6 + 6).toMatchInlineSnapshot(`12`) } diff --git a/test/snapshots/test/reuse.test.ts b/test/snapshots/test/reuse.test.ts index 9c3510b37356..d90c3ccf083f 100644 --- a/test/snapshots/test/reuse.test.ts +++ b/test/snapshots/test/reuse.test.ts @@ -1,21 +1,20 @@ -import { expect, test } from "vitest"; +import { expect, test } from 'vitest' test('a', () => { - myTest1(); - myTest2(); -}); + myTest1() + myTest2() +}) test('b', () => { - myTest1(); - myTest2(); -}); - + myTest1() + myTest2() +}) function myTest1() { - expect(7 + 7).toMatchInlineSnapshot(`14`); + expect(7 + 7).toMatchInlineSnapshot(`14`) } function myTest2() { - expect(3 + 3).toMatchInlineSnapshot(`6`); - expect(4 + 4).toMatchInlineSnapshot(`8`); -} \ No newline at end of file + expect(3 + 3).toMatchInlineSnapshot(`6`) + expect(4 + 4).toMatchInlineSnapshot(`8`) +} From c3423e8ceeb86552ad5b981609743a941e44aeb3 Mon Sep 17 00:00:00 2001 From: jycouet Date: Wed, 12 Feb 2025 11:27:42 +0100 Subject: [PATCH 09/24] lint++ --- .../test-update/inline-reuse.test.js | 1 + .../__snapshots__/test-update.test.ts.snap | 30 +++++++++---------- .../fixtures/test-update/inline-reuse.test.js | 30 +++++++++---------- 3 files changed, 31 insertions(+), 30 deletions(-) diff --git a/test/snapshots/test-update/inline-reuse.test.js b/test/snapshots/test-update/inline-reuse.test.js index ba2ed2af60f6..72eeb6b5c331 100644 --- a/test/snapshots/test-update/inline-reuse.test.js +++ b/test/snapshots/test-update/inline-reuse.test.js @@ -14,6 +14,7 @@ test('b', () => { myTest3() }) + function myTest1() { expect(7 + 7).toMatchInlineSnapshot(`14`) } diff --git a/test/snapshots/test/__snapshots__/test-update.test.ts.snap b/test/snapshots/test/__snapshots__/test-update.test.ts.snap index c4efa048c4d5..8fecab116980 100644 --- a/test/snapshots/test/__snapshots__/test-update.test.ts.snap +++ b/test/snapshots/test/__snapshots__/test-update.test.ts.snap @@ -91,36 +91,36 @@ it.concurrent('3rd', ({ expect }) => { `; exports[`inline-reuse.test.js 1`] = ` -"import { expect, test } from "vitest"; +"import { expect, test } from 'vitest' // when snapshots are generated Vitest reruns \`toMatchInlineSnapshot\` checks // please, don't commit generated snapshots test('a', () => { - myTest1(); - myTest2(); - myTest3(); -}); + myTest1() + myTest2() + myTest3() +}) test('b', () => { - myTest1(); - myTest2(); - myTest3(); -}); + myTest1() + myTest2() + myTest3() +}) function myTest1() { - expect(7 + 7).toMatchInlineSnapshot(\`14\`); + expect(7 + 7).toMatchInlineSnapshot(\`14\`) } function myTest2() { - expect(3 + 3).toMatchInlineSnapshot(\`6\`); - expect(4 + 4).toMatchInlineSnapshot(\`8\`); + expect(3 + 3).toMatchInlineSnapshot(\`6\`) + expect(4 + 4).toMatchInlineSnapshot(\`8\`) } function myTest3() { - expect(5 + 5).toMatchInlineSnapshot(\`10\`); - expect(5 + 6).toMatchInlineSnapshot(\`11\`); - expect(6 + 6).toMatchInlineSnapshot(\`12\`); + expect(5 + 5).toMatchInlineSnapshot(\`10\`) + expect(5 + 6).toMatchInlineSnapshot(\`11\`) + expect(6 + 6).toMatchInlineSnapshot(\`12\`) } " `; diff --git a/test/snapshots/test/fixtures/test-update/inline-reuse.test.js b/test/snapshots/test/fixtures/test-update/inline-reuse.test.js index 805d1b830600..bc4e708e4096 100644 --- a/test/snapshots/test/fixtures/test-update/inline-reuse.test.js +++ b/test/snapshots/test/fixtures/test-update/inline-reuse.test.js @@ -1,31 +1,31 @@ -import { expect, test } from "vitest"; +import { expect, test } from 'vitest' // when snapshots are generated Vitest reruns `toMatchInlineSnapshot` checks // please, don't commit generated snapshots test('a', () => { - myTest1(); - myTest2(); - myTest3(); -}); + myTest1() + myTest2() + myTest3() +}) test('b', () => { - myTest1(); - myTest2(); - myTest3(); -}); + myTest1() + myTest2() + myTest3() +}) function myTest1() { - expect(7 + 7).toMatchInlineSnapshot(); + expect(7 + 7).toMatchInlineSnapshot() } function myTest2() { - expect(3 + 3).toMatchInlineSnapshot(); - expect(4 + 4).toMatchInlineSnapshot(); + expect(3 + 3).toMatchInlineSnapshot() + expect(4 + 4).toMatchInlineSnapshot() } function myTest3() { - expect(5 + 5).toMatchInlineSnapshot(`"10"`); - expect(5 + 6).toMatchInlineSnapshot(); - expect(6 + 6).toMatchInlineSnapshot(`"12"`); + expect(5 + 5).toMatchInlineSnapshot(`"10"`) + expect(5 + 6).toMatchInlineSnapshot() + expect(6 + 6).toMatchInlineSnapshot(`"12"`) } From a77cb34c4fec72a5551c06618b94f47f16180f50 Mon Sep 17 00:00:00 2001 From: jycouet Date: Wed, 12 Feb 2025 11:35:51 +0100 Subject: [PATCH 10/24] lint# --- devbox.json | 17 +++ devbox.lock | 118 ++++++++++++++++++ .../test-update/inline-reuse.test.js | 1 - .../__snapshots__/test-update.test.ts.snap | 1 - .../fixtures/test-update/inline-reuse.test.js | 1 - 5 files changed, 135 insertions(+), 3 deletions(-) create mode 100644 devbox.json create mode 100644 devbox.lock diff --git a/devbox.json b/devbox.json new file mode 100644 index 000000000000..a3280af6f010 --- /dev/null +++ b/devbox.json @@ -0,0 +1,17 @@ +{ + "$schema": "https://raw.githubusercontent.com/jetify-com/devbox/0.13.7/.schema/devbox.schema.json", + "packages": [ + "nodejs@22", + "nodePackages.pnpm@9" + ], + "shell": { + "init_hook": [ + "echo 'Welcome to devbox!' > /dev/null" + ], + "scripts": { + "test": [ + "echo \"Error: no test specified\" && exit 1" + ] + } + } +} diff --git a/devbox.lock b/devbox.lock new file mode 100644 index 000000000000..2417c6aa481b --- /dev/null +++ b/devbox.lock @@ -0,0 +1,118 @@ +{ + "lockfile_version": "1", + "packages": { + "nodePackages.pnpm@9": { + "last_modified": "2024-06-26T05:30:43Z", + "resolved": "github:NixOS/nixpkgs/b3f3c1b13fb08f3828442ee86630362e81136bbc#nodePackages.pnpm", + "source": "devbox-search", + "version": "9.3.0", + "systems": { + "aarch64-darwin": { + "outputs": [ + { + "name": "out", + "path": "/nix/store/g2rnwxx2lrhshx7w8aghanc3y0mznnbg-pnpm-9.3.0", + "default": true + } + ], + "store_path": "/nix/store/g2rnwxx2lrhshx7w8aghanc3y0mznnbg-pnpm-9.3.0" + }, + "aarch64-linux": { + "outputs": [ + { + "name": "out", + "path": "/nix/store/ipnzflbc30xf7bfj7zswxcwp4yk89502-pnpm-9.3.0", + "default": true + } + ], + "store_path": "/nix/store/ipnzflbc30xf7bfj7zswxcwp4yk89502-pnpm-9.3.0" + }, + "x86_64-darwin": { + "outputs": [ + { + "name": "out", + "path": "/nix/store/0z47pn0raipavaqrq4gxarrz6dl53ns8-pnpm-9.3.0", + "default": true + } + ], + "store_path": "/nix/store/0z47pn0raipavaqrq4gxarrz6dl53ns8-pnpm-9.3.0" + }, + "x86_64-linux": { + "outputs": [ + { + "name": "out", + "path": "/nix/store/r0xwws8a6r6rnknsvgic1zgwf2p8kfkm-pnpm-9.3.0", + "default": true + } + ], + "store_path": "/nix/store/r0xwws8a6r6rnknsvgic1zgwf2p8kfkm-pnpm-9.3.0" + } + } + }, + "nodejs@22": { + "last_modified": "2025-02-07T11:26:36Z", + "plugin_version": "0.0.2", + "resolved": "github:NixOS/nixpkgs/d98abf5cf5914e5e4e9d57205e3af55ca90ffc1d#nodejs_22", + "source": "devbox-search", + "version": "22.13.1", + "systems": { + "aarch64-darwin": { + "outputs": [ + { + "name": "out", + "path": "/nix/store/iqv9xkigz40p94zi0dgy3g21iybyvpkk-nodejs-22.13.1", + "default": true + }, + { + "name": "libv8", + "path": "/nix/store/imvfvqlxvwkdacjxln49hii8f3shir90-nodejs-22.13.1-libv8" + } + ], + "store_path": "/nix/store/iqv9xkigz40p94zi0dgy3g21iybyvpkk-nodejs-22.13.1" + }, + "aarch64-linux": { + "outputs": [ + { + "name": "out", + "path": "/nix/store/77b6bhrkij6qdgl24bb33gpx6l7ijxpx-nodejs-22.13.1", + "default": true + }, + { + "name": "libv8", + "path": "/nix/store/3ry5lqz1126p39bmr6jc3h6qznbcaiqv-nodejs-22.13.1-libv8" + } + ], + "store_path": "/nix/store/77b6bhrkij6qdgl24bb33gpx6l7ijxpx-nodejs-22.13.1" + }, + "x86_64-darwin": { + "outputs": [ + { + "name": "out", + "path": "/nix/store/j3mzln68f98vbkhxwb8gi06krwawnyrw-nodejs-22.13.1", + "default": true + }, + { + "name": "libv8", + "path": "/nix/store/r7h7c7i2qb3c7zrqpnyfj2dk1b1f7qvg-nodejs-22.13.1-libv8" + } + ], + "store_path": "/nix/store/j3mzln68f98vbkhxwb8gi06krwawnyrw-nodejs-22.13.1" + }, + "x86_64-linux": { + "outputs": [ + { + "name": "out", + "path": "/nix/store/i1jdnip70qb7yh4krlzsgyxs0zdvw7xv-nodejs-22.13.1", + "default": true + }, + { + "name": "libv8", + "path": "/nix/store/6rnv25jp607ibr9k16paw776drvav77f-nodejs-22.13.1-libv8" + } + ], + "store_path": "/nix/store/i1jdnip70qb7yh4krlzsgyxs0zdvw7xv-nodejs-22.13.1" + } + } + } + } +} diff --git a/test/snapshots/test-update/inline-reuse.test.js b/test/snapshots/test-update/inline-reuse.test.js index 72eeb6b5c331..ba2ed2af60f6 100644 --- a/test/snapshots/test-update/inline-reuse.test.js +++ b/test/snapshots/test-update/inline-reuse.test.js @@ -14,7 +14,6 @@ test('b', () => { myTest3() }) - function myTest1() { expect(7 + 7).toMatchInlineSnapshot(`14`) } diff --git a/test/snapshots/test/__snapshots__/test-update.test.ts.snap b/test/snapshots/test/__snapshots__/test-update.test.ts.snap index 8fecab116980..8b776c5d4632 100644 --- a/test/snapshots/test/__snapshots__/test-update.test.ts.snap +++ b/test/snapshots/test/__snapshots__/test-update.test.ts.snap @@ -107,7 +107,6 @@ test('b', () => { myTest3() }) - function myTest1() { expect(7 + 7).toMatchInlineSnapshot(\`14\`) } diff --git a/test/snapshots/test/fixtures/test-update/inline-reuse.test.js b/test/snapshots/test/fixtures/test-update/inline-reuse.test.js index bc4e708e4096..171cf846f194 100644 --- a/test/snapshots/test/fixtures/test-update/inline-reuse.test.js +++ b/test/snapshots/test/fixtures/test-update/inline-reuse.test.js @@ -14,7 +14,6 @@ test('b', () => { myTest3() }) - function myTest1() { expect(7 + 7).toMatchInlineSnapshot() } From 76459369531ee9ae3dfd17f3322ee88868e13d73 Mon Sep 17 00:00:00 2001 From: jycouet Date: Wed, 12 Feb 2025 15:33:22 +0100 Subject: [PATCH 11/24] rmv devbox --- devbox.json | 17 -------- devbox.lock | 118 ---------------------------------------------------- 2 files changed, 135 deletions(-) delete mode 100644 devbox.json delete mode 100644 devbox.lock diff --git a/devbox.json b/devbox.json deleted file mode 100644 index a3280af6f010..000000000000 --- a/devbox.json +++ /dev/null @@ -1,17 +0,0 @@ -{ - "$schema": "https://raw.githubusercontent.com/jetify-com/devbox/0.13.7/.schema/devbox.schema.json", - "packages": [ - "nodejs@22", - "nodePackages.pnpm@9" - ], - "shell": { - "init_hook": [ - "echo 'Welcome to devbox!' > /dev/null" - ], - "scripts": { - "test": [ - "echo \"Error: no test specified\" && exit 1" - ] - } - } -} diff --git a/devbox.lock b/devbox.lock deleted file mode 100644 index 2417c6aa481b..000000000000 --- a/devbox.lock +++ /dev/null @@ -1,118 +0,0 @@ -{ - "lockfile_version": "1", - "packages": { - "nodePackages.pnpm@9": { - "last_modified": "2024-06-26T05:30:43Z", - "resolved": "github:NixOS/nixpkgs/b3f3c1b13fb08f3828442ee86630362e81136bbc#nodePackages.pnpm", - "source": "devbox-search", - "version": "9.3.0", - "systems": { - "aarch64-darwin": { - "outputs": [ - { - "name": "out", - "path": "/nix/store/g2rnwxx2lrhshx7w8aghanc3y0mznnbg-pnpm-9.3.0", - "default": true - } - ], - "store_path": "/nix/store/g2rnwxx2lrhshx7w8aghanc3y0mznnbg-pnpm-9.3.0" - }, - "aarch64-linux": { - "outputs": [ - { - "name": "out", - "path": "/nix/store/ipnzflbc30xf7bfj7zswxcwp4yk89502-pnpm-9.3.0", - "default": true - } - ], - "store_path": "/nix/store/ipnzflbc30xf7bfj7zswxcwp4yk89502-pnpm-9.3.0" - }, - "x86_64-darwin": { - "outputs": [ - { - "name": "out", - "path": "/nix/store/0z47pn0raipavaqrq4gxarrz6dl53ns8-pnpm-9.3.0", - "default": true - } - ], - "store_path": "/nix/store/0z47pn0raipavaqrq4gxarrz6dl53ns8-pnpm-9.3.0" - }, - "x86_64-linux": { - "outputs": [ - { - "name": "out", - "path": "/nix/store/r0xwws8a6r6rnknsvgic1zgwf2p8kfkm-pnpm-9.3.0", - "default": true - } - ], - "store_path": "/nix/store/r0xwws8a6r6rnknsvgic1zgwf2p8kfkm-pnpm-9.3.0" - } - } - }, - "nodejs@22": { - "last_modified": "2025-02-07T11:26:36Z", - "plugin_version": "0.0.2", - "resolved": "github:NixOS/nixpkgs/d98abf5cf5914e5e4e9d57205e3af55ca90ffc1d#nodejs_22", - "source": "devbox-search", - "version": "22.13.1", - "systems": { - "aarch64-darwin": { - "outputs": [ - { - "name": "out", - "path": "/nix/store/iqv9xkigz40p94zi0dgy3g21iybyvpkk-nodejs-22.13.1", - "default": true - }, - { - "name": "libv8", - "path": "/nix/store/imvfvqlxvwkdacjxln49hii8f3shir90-nodejs-22.13.1-libv8" - } - ], - "store_path": "/nix/store/iqv9xkigz40p94zi0dgy3g21iybyvpkk-nodejs-22.13.1" - }, - "aarch64-linux": { - "outputs": [ - { - "name": "out", - "path": "/nix/store/77b6bhrkij6qdgl24bb33gpx6l7ijxpx-nodejs-22.13.1", - "default": true - }, - { - "name": "libv8", - "path": "/nix/store/3ry5lqz1126p39bmr6jc3h6qznbcaiqv-nodejs-22.13.1-libv8" - } - ], - "store_path": "/nix/store/77b6bhrkij6qdgl24bb33gpx6l7ijxpx-nodejs-22.13.1" - }, - "x86_64-darwin": { - "outputs": [ - { - "name": "out", - "path": "/nix/store/j3mzln68f98vbkhxwb8gi06krwawnyrw-nodejs-22.13.1", - "default": true - }, - { - "name": "libv8", - "path": "/nix/store/r7h7c7i2qb3c7zrqpnyfj2dk1b1f7qvg-nodejs-22.13.1-libv8" - } - ], - "store_path": "/nix/store/j3mzln68f98vbkhxwb8gi06krwawnyrw-nodejs-22.13.1" - }, - "x86_64-linux": { - "outputs": [ - { - "name": "out", - "path": "/nix/store/i1jdnip70qb7yh4krlzsgyxs0zdvw7xv-nodejs-22.13.1", - "default": true - }, - { - "name": "libv8", - "path": "/nix/store/6rnv25jp607ibr9k16paw776drvav77f-nodejs-22.13.1-libv8" - } - ], - "store_path": "/nix/store/i1jdnip70qb7yh4krlzsgyxs0zdvw7xv-nodejs-22.13.1" - } - } - } - } -} From 686c94d7dda2956d45cddabc41d829daf694623c Mon Sep 17 00:00:00 2001 From: jycouet Date: Wed, 12 Feb 2025 15:49:14 +0100 Subject: [PATCH 12/24] add --- 1devbox.json | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) create mode 100644 1devbox.json diff --git a/1devbox.json b/1devbox.json new file mode 100644 index 000000000000..a3280af6f010 --- /dev/null +++ b/1devbox.json @@ -0,0 +1,17 @@ +{ + "$schema": "https://raw.githubusercontent.com/jetify-com/devbox/0.13.7/.schema/devbox.schema.json", + "packages": [ + "nodejs@22", + "nodePackages.pnpm@9" + ], + "shell": { + "init_hook": [ + "echo 'Welcome to devbox!' > /dev/null" + ], + "scripts": { + "test": [ + "echo \"Error: no test specified\" && exit 1" + ] + } + } +} From 42e42b6c8d373980c183ffa017fb55ffbdc512b5 Mon Sep 17 00:00:00 2001 From: jycouet Date: Wed, 12 Feb 2025 15:49:40 +0100 Subject: [PATCH 13/24] del --- 1devbox.json | 17 ----------------- 1 file changed, 17 deletions(-) delete mode 100644 1devbox.json diff --git a/1devbox.json b/1devbox.json deleted file mode 100644 index a3280af6f010..000000000000 --- a/1devbox.json +++ /dev/null @@ -1,17 +0,0 @@ -{ - "$schema": "https://raw.githubusercontent.com/jetify-com/devbox/0.13.7/.schema/devbox.schema.json", - "packages": [ - "nodejs@22", - "nodePackages.pnpm@9" - ], - "shell": { - "init_hook": [ - "echo 'Welcome to devbox!' > /dev/null" - ], - "scripts": { - "test": [ - "echo \"Error: no test specified\" && exit 1" - ] - } - } -} From 8e6ec5e2e0141c06b84dd74b8b72a699e23c61f7 Mon Sep 17 00:00:00 2001 From: Hiroshi Ogawa Date: Mon, 17 Feb 2025 14:48:09 +0900 Subject: [PATCH 14/24] test: add test --- .../inline-multiple-calls/basic.test.ts | 7 ++ .../test/inline-multiple-calls.test.ts | 92 +++++++++++++++++++ 2 files changed, 99 insertions(+) create mode 100644 test/snapshots/test/fixtures/inline-multiple-calls/basic.test.ts create mode 100644 test/snapshots/test/inline-multiple-calls.test.ts diff --git a/test/snapshots/test/fixtures/inline-multiple-calls/basic.test.ts b/test/snapshots/test/fixtures/inline-multiple-calls/basic.test.ts new file mode 100644 index 000000000000..c4f32383fc23 --- /dev/null +++ b/test/snapshots/test/fixtures/inline-multiple-calls/basic.test.ts @@ -0,0 +1,7 @@ +import { expect, test } from 'vitest' + +test('test1', () => { + for (const test1 of ["test1", "test1"]) { + expect(test1).toMatchInlineSnapshot(`"test1"`) + } +}) diff --git a/test/snapshots/test/inline-multiple-calls.test.ts b/test/snapshots/test/inline-multiple-calls.test.ts new file mode 100644 index 000000000000..3631c769192d --- /dev/null +++ b/test/snapshots/test/inline-multiple-calls.test.ts @@ -0,0 +1,92 @@ +import fs from 'node:fs' +import { join } from 'node:path' +import { expect, test } from 'vitest' +import { editFile, runVitest } from '../../test-utils' + +// pnpm -C test/snapshots test:fixtures --root test/fixtures/inline-multiple-calls +// pnpm -C test/snapshots test:snaps inline-multiple-calls + +test('workflow', async () => { + // reset snapshot + const root = join(import.meta.dirname, 'fixtures/inline-multiple-calls') + const testFile = join(root, 'basic.test.ts') + editFile(testFile, s => s.replace(/toMatchInlineSnapshot\(`.*`\)/gs, 'toMatchInlineSnapshot()')) + + // iniital run (create snapshot) + let vitest = await runVitest({ + root, + update: true, + }) + expect(vitest.stderr).toBe('') + // TODO: should summary have `added: 1`? + expect(vitest.ctx?.snapshot.summary).toMatchInlineSnapshot(` + Object { + "added": 2, + "didUpdate": true, + "failure": false, + "filesAdded": 1, + "filesRemoved": 0, + "filesRemovedList": Array [], + "filesUnmatched": 0, + "filesUpdated": 0, + "matched": 0, + "total": 2, + "unchecked": 0, + "uncheckedKeysByFile": Array [], + "unmatched": 0, + "updated": 0, + } + `) + expect(fs.readFileSync(testFile, 'utf-8')).toContain('expect(test1).toMatchInlineSnapshot(`"test1"`)') + + // no-update run + vitest = await runVitest({ + root, + update: false, + }) + expect(vitest.stderr).toBe('') + expect(vitest.ctx?.snapshot.summary).toMatchInlineSnapshot(` + Object { + "added": 0, + "didUpdate": false, + "failure": false, + "filesAdded": 0, + "filesRemoved": 0, + "filesRemovedList": Array [], + "filesUnmatched": 0, + "filesUpdated": 0, + "matched": 2, + "total": 2, + "unchecked": 0, + "uncheckedKeysByFile": Array [], + "unmatched": 0, + "updated": 0, + } + `) + expect(fs.readFileSync(testFile, 'utf-8')).toContain('expect(test1).toMatchInlineSnapshot(`"test1"`)') + + // update run + vitest = await runVitest({ + root, + update: true, + }) + expect(vitest.ctx?.snapshot.summary).toMatchInlineSnapshot(` + Object { + "added": 0, + "didUpdate": true, + "failure": false, + "filesAdded": 0, + "filesRemoved": 0, + "filesRemovedList": Array [], + "filesUnmatched": 0, + "filesUpdated": 0, + "matched": 2, + "total": 2, + "unchecked": 0, + "uncheckedKeysByFile": Array [], + "unmatched": 0, + "updated": 0, + } + `) + expect(fs.readFileSync(testFile, 'utf-8')).toContain('expect(test1).toMatchInlineSnapshot(`"test1"`)') +}) From 188e1ca1e4c85f09312833582fc2a94ce17ec5e6 Mon Sep 17 00:00:00 2001 From: Hiroshi Ogawa Date: Mon, 17 Feb 2025 14:50:19 +0900 Subject: [PATCH 15/24] test: remove simple tests --- .../test-update/inline-reuse.test.js | 30 ---------------- .../__snapshots__/test-update.test.ts.snap | 34 ------------------- .../fixtures/test-update/inline-reuse.test.js | 30 ---------------- test/snapshots/test/reuse.test.ts | 20 ----------- 4 files changed, 114 deletions(-) delete mode 100644 test/snapshots/test-update/inline-reuse.test.js delete mode 100644 test/snapshots/test/fixtures/test-update/inline-reuse.test.js delete mode 100644 test/snapshots/test/reuse.test.ts diff --git a/test/snapshots/test-update/inline-reuse.test.js b/test/snapshots/test-update/inline-reuse.test.js deleted file mode 100644 index ba2ed2af60f6..000000000000 --- a/test/snapshots/test-update/inline-reuse.test.js +++ /dev/null @@ -1,30 +0,0 @@ -import { expect, test } from 'vitest' - -// when snapshots are generated Vitest reruns `toMatchInlineSnapshot` checks -// please, don't commit generated snapshots -test('a', () => { - myTest1() - myTest2() - myTest3() -}) - -test('b', () => { - myTest1() - myTest2() - myTest3() -}) - -function myTest1() { - expect(7 + 7).toMatchInlineSnapshot(`14`) -} - -function myTest2() { - expect(3 + 3).toMatchInlineSnapshot(`6`) - expect(4 + 4).toMatchInlineSnapshot(`8`) -} - -function myTest3() { - expect(5 + 5).toMatchInlineSnapshot(`10`) - expect(5 + 6).toMatchInlineSnapshot(`11`) - expect(6 + 6).toMatchInlineSnapshot(`12`) -} diff --git a/test/snapshots/test/__snapshots__/test-update.test.ts.snap b/test/snapshots/test/__snapshots__/test-update.test.ts.snap index 8b776c5d4632..3321ee7ef68e 100644 --- a/test/snapshots/test/__snapshots__/test-update.test.ts.snap +++ b/test/snapshots/test/__snapshots__/test-update.test.ts.snap @@ -90,40 +90,6 @@ it.concurrent('3rd', ({ expect }) => { " `; -exports[`inline-reuse.test.js 1`] = ` -"import { expect, test } from 'vitest' - -// when snapshots are generated Vitest reruns \`toMatchInlineSnapshot\` checks -// please, don't commit generated snapshots -test('a', () => { - myTest1() - myTest2() - myTest3() -}) - -test('b', () => { - myTest1() - myTest2() - myTest3() -}) - -function myTest1() { - expect(7 + 7).toMatchInlineSnapshot(\`14\`) -} - -function myTest2() { - expect(3 + 3).toMatchInlineSnapshot(\`6\`) - expect(4 + 4).toMatchInlineSnapshot(\`8\`) -} - -function myTest3() { - expect(5 + 5).toMatchInlineSnapshot(\`10\`) - expect(5 + 6).toMatchInlineSnapshot(\`11\`) - expect(6 + 6).toMatchInlineSnapshot(\`12\`) -} -" -`; - exports[`retry-inline.test.ts 1`] = ` "import { expect, test } from 'vitest' diff --git a/test/snapshots/test/fixtures/test-update/inline-reuse.test.js b/test/snapshots/test/fixtures/test-update/inline-reuse.test.js deleted file mode 100644 index 171cf846f194..000000000000 --- a/test/snapshots/test/fixtures/test-update/inline-reuse.test.js +++ /dev/null @@ -1,30 +0,0 @@ -import { expect, test } from 'vitest' - -// when snapshots are generated Vitest reruns `toMatchInlineSnapshot` checks -// please, don't commit generated snapshots -test('a', () => { - myTest1() - myTest2() - myTest3() -}) - -test('b', () => { - myTest1() - myTest2() - myTest3() -}) - -function myTest1() { - expect(7 + 7).toMatchInlineSnapshot() -} - -function myTest2() { - expect(3 + 3).toMatchInlineSnapshot() - expect(4 + 4).toMatchInlineSnapshot() -} - -function myTest3() { - expect(5 + 5).toMatchInlineSnapshot(`"10"`) - expect(5 + 6).toMatchInlineSnapshot() - expect(6 + 6).toMatchInlineSnapshot(`"12"`) -} diff --git a/test/snapshots/test/reuse.test.ts b/test/snapshots/test/reuse.test.ts deleted file mode 100644 index d90c3ccf083f..000000000000 --- a/test/snapshots/test/reuse.test.ts +++ /dev/null @@ -1,20 +0,0 @@ -import { expect, test } from 'vitest' - -test('a', () => { - myTest1() - myTest2() -}) - -test('b', () => { - myTest1() - myTest2() -}) - -function myTest1() { - expect(7 + 7).toMatchInlineSnapshot(`14`) -} - -function myTest2() { - expect(3 + 3).toMatchInlineSnapshot(`6`) - expect(4 + 4).toMatchInlineSnapshot(`8`) -} From 01030c043f785a78eab3d4b886eb0588b5129a43 Mon Sep 17 00:00:00 2001 From: Hiroshi Ogawa Date: Mon, 17 Feb 2025 15:12:01 +0900 Subject: [PATCH 16/24] test: add failing test --- .../inline-multiple-calls/different.test.ts | 7 ++ .../{basic.test.ts => same.test.ts} | 0 .../test/inline-multiple-calls.test.ts | 64 ++++++++++++++++++- 3 files changed, 68 insertions(+), 3 deletions(-) create mode 100644 test/snapshots/test/fixtures/inline-multiple-calls/different.test.ts rename test/snapshots/test/fixtures/inline-multiple-calls/{basic.test.ts => same.test.ts} (100%) diff --git a/test/snapshots/test/fixtures/inline-multiple-calls/different.test.ts b/test/snapshots/test/fixtures/inline-multiple-calls/different.test.ts new file mode 100644 index 000000000000..88ae95cd271f --- /dev/null +++ b/test/snapshots/test/fixtures/inline-multiple-calls/different.test.ts @@ -0,0 +1,7 @@ +import { expect, test } from 'vitest' + +test('test1', () => { + for (const test1 of ["test1", "test2"]) { + expect(test1).toMatchInlineSnapshot() + } +}) diff --git a/test/snapshots/test/fixtures/inline-multiple-calls/basic.test.ts b/test/snapshots/test/fixtures/inline-multiple-calls/same.test.ts similarity index 100% rename from test/snapshots/test/fixtures/inline-multiple-calls/basic.test.ts rename to test/snapshots/test/fixtures/inline-multiple-calls/same.test.ts diff --git a/test/snapshots/test/inline-multiple-calls.test.ts b/test/snapshots/test/inline-multiple-calls.test.ts index 3631c769192d..aae89123ae91 100644 --- a/test/snapshots/test/inline-multiple-calls.test.ts +++ b/test/snapshots/test/inline-multiple-calls.test.ts @@ -3,18 +3,20 @@ import { join } from 'node:path' import { expect, test } from 'vitest' import { editFile, runVitest } from '../../test-utils' -// pnpm -C test/snapshots test:fixtures --root test/fixtures/inline-multiple-calls // pnpm -C test/snapshots test:snaps inline-multiple-calls -test('workflow', async () => { +test('same', async () => { + // pnpm -C test/snapshots test:fixtures --root test/fixtures/inline-multiple-calls same + // reset snapshot const root = join(import.meta.dirname, 'fixtures/inline-multiple-calls') - const testFile = join(root, 'basic.test.ts') + const testFile = join(root, 'same.test.ts') editFile(testFile, s => s.replace(/toMatchInlineSnapshot\(`.*`\)/gs, 'toMatchInlineSnapshot()')) // iniital run (create snapshot) let vitest = await runVitest({ root, + include: [testFile], update: true, }) expect(vitest.stderr).toBe('') @@ -42,6 +44,7 @@ test('workflow', async () => { // no-update run vitest = await runVitest({ root, + include: [testFile], update: false, }) expect(vitest.stderr).toBe('') @@ -68,6 +71,7 @@ test('workflow', async () => { // update run vitest = await runVitest({ root, + include: [testFile], update: true, }) expect(vitest.ctx?.snapshot.summary).toMatchInlineSnapshot(` @@ -90,3 +94,57 @@ test('workflow', async () => { `) expect(fs.readFileSync(testFile, 'utf-8')).toContain('expect(test1).toMatchInlineSnapshot(`"test1"`)') }) + +test('different', async () => { + // pnpm -C test/snapshots test:fixtures --root test/fixtures/inline-multiple-calls different + + // 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.soft(vitest.exitCode).not.toBe(0) + expect(fs.readFileSync(testFile, 'utf-8')).toContain('expect(test1).toMatchInlineSnapshot()') + + // no-update run should fail + vitest = await runVitest({ + root, + include: [testFile], + update: false, + }) + expect(vitest.exitCode).not.toBe(0) + expect(fs.readFileSync(testFile, 'utf-8')).toContain('expect(test1).toMatchInlineSnapshot()') + + // current snapshot is "test1" + editFile(testFile, s => s.replace('expect(test1).toMatchInlineSnapshot()', 'expect(test1).toMatchInlineSnapshot(`"test1"`)')) + vitest = await runVitest({ + root, + include: [testFile], + update: true, + }) + expect(vitest.exitCode).not.toBe(0) + + // current snapshot is "test2" + editFile(testFile, s => s.replace('expect(test1).toMatchInlineSnapshot()', 'expect(test1).toMatchInlineSnapshot(`"test1"`)')) + vitest = await runVitest({ + root, + include: [testFile], + update: true, + }) + expect(vitest.exitCode).not.toBe(0) + + // current snapshot is "test3" + editFile(testFile, s => s.replace('expect(test1).toMatchInlineSnapshot()', 'expect(test1).toMatchInlineSnapshot(`"test1"`)')) + vitest = await runVitest({ + root, + include: [testFile], + update: true, + }) + expect(vitest.exitCode).not.toBe(0) +}) From 1dc1c63ee4415a42405772e9fb034d8e97e3974c Mon Sep 17 00:00:00 2001 From: Hiroshi Ogawa Date: Mon, 17 Feb 2025 15:13:16 +0900 Subject: [PATCH 17/24] test: remove old test --- .../cli/fixtures/fails/inline-snapshop-inside-loop.test.ts | 7 ------- test/cli/test/__snapshots__/fails.test.ts.snap | 2 -- 2 files changed, 9 deletions(-) delete mode 100644 test/cli/fixtures/fails/inline-snapshop-inside-loop.test.ts diff --git a/test/cli/fixtures/fails/inline-snapshop-inside-loop.test.ts b/test/cli/fixtures/fails/inline-snapshop-inside-loop.test.ts deleted file mode 100644 index 7dad57e0fe24..000000000000 --- a/test/cli/fixtures/fails/inline-snapshop-inside-loop.test.ts +++ /dev/null @@ -1,7 +0,0 @@ -import {test, expect} from "vitest"; - -test("fail 1", () => { - for (const str of ["foo", "bar"]) { - expect(str).toMatchInlineSnapshot(`"foo"`); - } -}); \ No newline at end of file diff --git a/test/cli/test/__snapshots__/fails.test.ts.snap b/test/cli/test/__snapshots__/fails.test.ts.snap index 4fc59498570f..e75221b82057 100644 --- a/test/cli/test/__snapshots__/fails.test.ts.snap +++ b/test/cli/test/__snapshots__/fails.test.ts.snap @@ -50,8 +50,6 @@ Error: InlineSnapshot cannot be used inside of test.each or describe.each Error: InlineSnapshot cannot be used inside of test.each or describe.each" `; -exports[`should fail inline-snapshop-inside-loop.test.ts 1`] = `"Error: Snapshot \`fail 1 2\` mismatched"`; - exports[`should fail mock-import-proxy-module.test.ts 1`] = `"Error: There are some problems in resolving the mocks API."`; exports[`should fail nested-suite.test.ts 1`] = `"AssertionError: expected true to be false // Object.is equality"`; From 389f83f704ef935f6c3170e5ac93fd42d4a1545f Mon Sep 17 00:00:00 2001 From: Hiroshi Ogawa Date: Mon, 17 Feb 2025 15:21:05 +0900 Subject: [PATCH 18/24] test: tweak --- test/snapshots/test/inline-multiple-calls.test.ts | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/test/snapshots/test/inline-multiple-calls.test.ts b/test/snapshots/test/inline-multiple-calls.test.ts index aae89123ae91..8b142a2fd273 100644 --- a/test/snapshots/test/inline-multiple-calls.test.ts +++ b/test/snapshots/test/inline-multiple-calls.test.ts @@ -129,6 +129,15 @@ test('different', async () => { update: true, }) expect(vitest.exitCode).not.toBe(0) + expect(fs.readFileSync(testFile, 'utf-8')).toContain('expect(test1).toMatchInlineSnapshot(`"test1"`)') + + vitest = await runVitest({ + root, + include: [testFile], + update: false, + }) + expect(vitest.exitCode).not.toBe(0) + expect(fs.readFileSync(testFile, 'utf-8')).toContain('expect(test1).toMatchInlineSnapshot(`"test1"`)') // current snapshot is "test2" editFile(testFile, s => s.replace('expect(test1).toMatchInlineSnapshot()', 'expect(test1).toMatchInlineSnapshot(`"test1"`)')) @@ -138,13 +147,13 @@ test('different', async () => { update: true, }) expect(vitest.exitCode).not.toBe(0) + expect(fs.readFileSync(testFile, 'utf-8')).toContain('expect(test1).toMatchInlineSnapshot(`"test2"`)') - // current snapshot is "test3" - editFile(testFile, s => s.replace('expect(test1).toMatchInlineSnapshot()', 'expect(test1).toMatchInlineSnapshot(`"test1"`)')) vitest = await runVitest({ root, include: [testFile], - update: true, + update: false, }) expect(vitest.exitCode).not.toBe(0) + expect(fs.readFileSync(testFile, 'utf-8')).toContain('expect(test1).toMatchInlineSnapshot(`"test2"`)') }) From e3bb4604a39aaeaf0a2ead099490319d1a9935f5 Mon Sep 17 00:00:00 2001 From: Hiroshi Ogawa Date: Mon, 17 Feb 2025 16:07:17 +0900 Subject: [PATCH 19/24] fix: reject only when snapshot mismatches --- packages/snapshot/src/port/inlineSnapshot.ts | 23 +++++++++++-------- packages/snapshot/src/port/state.ts | 18 +++++++++++++-- .../test/inline-multiple-calls.test.ts | 6 ++--- 3 files changed, 33 insertions(+), 14 deletions(-) diff --git a/packages/snapshot/src/port/inlineSnapshot.ts b/packages/snapshot/src/port/inlineSnapshot.ts index 843cd35a9340..7dffa91bc6bd 100644 --- a/packages/snapshot/src/port/inlineSnapshot.ts +++ b/packages/snapshot/src/port/inlineSnapshot.ts @@ -27,15 +27,20 @@ export async function saveInlineSnapshots( const code = await environment.readSnapshotFile(file) as string const s = new MagicString(code) - const uniqueSnaps = snaps.reduce((acc: InlineSnapshot[], snap) => { - const exists = acc.some(s => s.line === snap.line && s.column === snap.column) - if (!exists) { - acc.push(snap) - } - return acc - }, []) - - for (const snap of uniqueSnaps) { + // const uniqueSnaps = snaps.reduce((acc: InlineSnapshot[], snap) => { + // const exists = acc.some(s => s.line === snap.line && s.column === snap.column) + // if (!exists) { + // acc.push(snap) + // } + // return acc + // }, []) + + // for (const snap of uniqueSnaps) { + // const index = positionToOffset(code, snap.line, snap.column) + // replaceInlineSnap(code, s, index, snap.snapshot) + // } + + for (const snap of snaps) { const index = positionToOffset(code, snap.line, snap.column) replaceInlineSnap(code, s, index, snap.snapshot) } diff --git a/packages/snapshot/src/port/state.ts b/packages/snapshot/src/port/state.ts index b3b12ea2a673..b6407ab989db 100644 --- a/packages/snapshot/src/port/state.ts +++ b/packages/snapshot/src/port/state.ts @@ -48,6 +48,12 @@ interface SaveStatus { saved: boolean } +type ParsedStackPosition = Pick + +function isSameStackPosition(x: ParsedStackPosition, y: ParsedStackPosition) { + return x.file === y.file && x.column === y.column && x.line === y.line +} + export default class SnapshotState { private _counters = new CounterMap() private _dirty: boolean @@ -55,7 +61,7 @@ export default class SnapshotState { private _snapshotData: SnapshotData private _initialData: SnapshotData private _inlineSnapshots: Array - private _inlineSnapshotStacks: Array + private _inlineSnapshotStacks: Array private _testIdToKeys = new DefaultMap(() => []) private _rawSnapshots: Array private _uncheckedKeys: Set @@ -343,7 +349,15 @@ export default class SnapshotState { // https://github.com/vitejs/vite/issues/8657 stack.column-- - this._inlineSnapshotStacks.push({ ...stack, testId }) + // ensure only one snapshot will be written at the same location + this._inlineSnapshots = this._inlineSnapshots.filter(s => !isSameStackPosition(s, stack!)) + + // reject multiple inline snapshots at the same location when snapshot is different + if (this._inlineSnapshotStacks.some(s => isSameStackPosition(s, stack!) && receivedSerialized !== s.snapshot)) { + // TODO: include diff in error + throw new Error('toMatchInlineSnapshot cannot be called multiple times at the same location with a different snapshot') + } + this._inlineSnapshotStacks.push({ ...stack, testId, snapshot: receivedSerialized }) } // These are the conditions on when to write snapshots: diff --git a/test/snapshots/test/inline-multiple-calls.test.ts b/test/snapshots/test/inline-multiple-calls.test.ts index 8b142a2fd273..ed64c572a00b 100644 --- a/test/snapshots/test/inline-multiple-calls.test.ts +++ b/test/snapshots/test/inline-multiple-calls.test.ts @@ -20,7 +20,6 @@ test('same', async () => { update: true, }) expect(vitest.stderr).toBe('') - // TODO: should summary have `added: 1`? expect(vitest.ctx?.snapshot.summary).toMatchInlineSnapshot(` Object { "added": 2, @@ -109,8 +108,9 @@ test('different', async () => { include: [testFile], update: true, }) - expect.soft(vitest.exitCode).not.toBe(0) + expect(vitest.stderr).toContain('toMatchInlineSnapshot cannot be called multiple times at the same location with a different snapshot') expect(fs.readFileSync(testFile, 'utf-8')).toContain('expect(test1).toMatchInlineSnapshot()') + expect(vitest.exitCode).not.toBe(0) // no-update run should fail vitest = await runVitest({ @@ -140,7 +140,7 @@ test('different', async () => { expect(fs.readFileSync(testFile, 'utf-8')).toContain('expect(test1).toMatchInlineSnapshot(`"test1"`)') // current snapshot is "test2" - editFile(testFile, s => s.replace('expect(test1).toMatchInlineSnapshot()', 'expect(test1).toMatchInlineSnapshot(`"test1"`)')) + editFile(testFile, s => s.replace('expect(test1).toMatchInlineSnapshot(`"test1"`)', 'expect(test1).toMatchInlineSnapshot(`"test2"`)')) vitest = await runVitest({ root, include: [testFile], From 20442c566149cb839bc89441d47eed369363390f Mon Sep 17 00:00:00 2001 From: Hiroshi Ogawa Date: Mon, 17 Feb 2025 16:08:10 +0900 Subject: [PATCH 20/24] chore: cleanup --- packages/snapshot/src/port/inlineSnapshot.ts | 13 ------------- 1 file changed, 13 deletions(-) diff --git a/packages/snapshot/src/port/inlineSnapshot.ts b/packages/snapshot/src/port/inlineSnapshot.ts index 7dffa91bc6bd..09f059661655 100644 --- a/packages/snapshot/src/port/inlineSnapshot.ts +++ b/packages/snapshot/src/port/inlineSnapshot.ts @@ -27,19 +27,6 @@ export async function saveInlineSnapshots( const code = await environment.readSnapshotFile(file) as string const s = new MagicString(code) - // const uniqueSnaps = snaps.reduce((acc: InlineSnapshot[], snap) => { - // const exists = acc.some(s => s.line === snap.line && s.column === snap.column) - // if (!exists) { - // acc.push(snap) - // } - // return acc - // }, []) - - // for (const snap of uniqueSnaps) { - // const index = positionToOffset(code, snap.line, snap.column) - // replaceInlineSnap(code, s, index, snap.snapshot) - // } - for (const snap of snaps) { const index = positionToOffset(code, snap.line, snap.column) replaceInlineSnap(code, s, index, snap.snapshot) From 0647a67414e9beaf852441338eb74bc8783bb85a Mon Sep 17 00:00:00 2001 From: Hiroshi Ogawa Date: Mon, 17 Feb 2025 16:26:18 +0900 Subject: [PATCH 21/24] fix: show diff --- packages/snapshot/src/port/state.ts | 25 +++++++---- .../test/inline-multiple-calls.test.ts | 42 ++++++++++++++++--- 2 files changed, 54 insertions(+), 13 deletions(-) diff --git a/packages/snapshot/src/port/state.ts b/packages/snapshot/src/port/state.ts index b6407ab989db..80ef8d4bd815 100644 --- a/packages/snapshot/src/port/state.ts +++ b/packages/snapshot/src/port/state.ts @@ -349,13 +349,24 @@ export default class SnapshotState { // https://github.com/vitejs/vite/issues/8657 stack.column-- - // ensure only one snapshot will be written at the same location - this._inlineSnapshots = this._inlineSnapshots.filter(s => !isSameStackPosition(s, stack!)) - - // reject multiple inline snapshots at the same location when snapshot is different - if (this._inlineSnapshotStacks.some(s => isSameStackPosition(s, stack!) && receivedSerialized !== s.snapshot)) { - // TODO: include diff in error - throw new Error('toMatchInlineSnapshot cannot be called multiple times at the same location with a different snapshot') + // reject multiple inline snapshots at the same location if snapshot is different + const snapshotsWithSameStack = this._inlineSnapshotStacks.filter(s => isSameStackPosition(s, stack!)) + if (snapshotsWithSameStack.length > 0) { + // ensure only one snapshot will be written at the same location + this._inlineSnapshots = this._inlineSnapshots.filter(s => !isSameStackPosition(s, stack!)) + + const differentSnapshot = snapshotsWithSameStack.find(s => s.snapshot !== receivedSerialized) + if (differentSnapshot) { + throw Object.assign( + new Error( + 'toMatchInlineSnapshot with different snapshots cannot be called at the same location', + ), + { + actual: receivedSerialized, + expected: differentSnapshot.snapshot, + }, + ) + } } this._inlineSnapshotStacks.push({ ...stack, testId, snapshot: receivedSerialized }) } diff --git a/test/snapshots/test/inline-multiple-calls.test.ts b/test/snapshots/test/inline-multiple-calls.test.ts index ed64c572a00b..67ed4ac4bc15 100644 --- a/test/snapshots/test/inline-multiple-calls.test.ts +++ b/test/snapshots/test/inline-multiple-calls.test.ts @@ -108,7 +108,12 @@ test('different', async () => { include: [testFile], update: true, }) - expect(vitest.stderr).toContain('toMatchInlineSnapshot cannot be called multiple times at the same location with a different snapshot') + 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(test1).toMatchInlineSnapshot()') expect(vitest.exitCode).not.toBe(0) @@ -118,7 +123,12 @@ test('different', async () => { include: [testFile], update: false, }) - expect(vitest.exitCode).not.toBe(0) + 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(test1).toMatchInlineSnapshot()') // current snapshot is "test1" @@ -128,7 +138,12 @@ test('different', async () => { include: [testFile], update: true, }) - expect(vitest.exitCode).not.toBe(0) + 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(test1).toMatchInlineSnapshot(`"test1"`)') vitest = await runVitest({ @@ -136,7 +151,12 @@ test('different', async () => { include: [testFile], update: false, }) - expect(vitest.exitCode).not.toBe(0) + 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(test1).toMatchInlineSnapshot(`"test1"`)') // current snapshot is "test2" @@ -146,7 +166,12 @@ test('different', async () => { include: [testFile], update: true, }) - expect(vitest.exitCode).not.toBe(0) + 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(test1).toMatchInlineSnapshot(`"test2"`)') vitest = await runVitest({ @@ -154,6 +179,11 @@ test('different', async () => { include: [testFile], update: false, }) - expect(vitest.exitCode).not.toBe(0) + expect(vitest.stderr).toContain(` +Error: Snapshot \`test1 1\` mismatched + +Expected: ""test2"" +Received: ""test1"" +`) expect(fs.readFileSync(testFile, 'utf-8')).toContain('expect(test1).toMatchInlineSnapshot(`"test2"`)') }) From 2bff43225adeff07f64f75b81e44ec5d3fe422ce Mon Sep 17 00:00:00 2001 From: Hiroshi Ogawa Date: Mon, 17 Feb 2025 16:31:28 +0900 Subject: [PATCH 22/24] test: pathe for windows --- test/snapshots/test/inline-multiple-calls.test.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/snapshots/test/inline-multiple-calls.test.ts b/test/snapshots/test/inline-multiple-calls.test.ts index 67ed4ac4bc15..ef9ccff26df8 100644 --- a/test/snapshots/test/inline-multiple-calls.test.ts +++ b/test/snapshots/test/inline-multiple-calls.test.ts @@ -1,5 +1,5 @@ import fs from 'node:fs' -import { join } from 'node:path' +import { join } from 'pathe' import { expect, test } from 'vitest' import { editFile, runVitest } from '../../test-utils' From 16b19920687f03c922151f5a8865b5304797c45b Mon Sep 17 00:00:00 2001 From: Hiroshi Ogawa Date: Mon, 17 Feb 2025 17:13:42 +0900 Subject: [PATCH 23/24] test: tweak for ci --- test/snapshots/test/inline-multiple-calls.test.ts | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/test/snapshots/test/inline-multiple-calls.test.ts b/test/snapshots/test/inline-multiple-calls.test.ts index ef9ccff26df8..78a70898e3f4 100644 --- a/test/snapshots/test/inline-multiple-calls.test.ts +++ b/test/snapshots/test/inline-multiple-calls.test.ts @@ -123,12 +123,19 @@ Received: ""test2"" include: [testFile], update: false, }) - expect(vitest.stderr).toContain(` + if (process.env.CI) { + expect(vitest.stderr).toContain(` +Error: Snapshot \`test1 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(test1).toMatchInlineSnapshot()') // current snapshot is "test1" From 66a124fe91893383631514c00db77e9155a84034 Mon Sep 17 00:00:00 2001 From: Hiroshi Ogawa Date: Mon, 17 Feb 2025 18:08:18 +0900 Subject: [PATCH 24/24] test: test multiple tests --- .../inline-multiple-calls/different.test.ts | 6 +- .../inline-multiple-calls/different2.test.ts | 13 + .../inline-multiple-calls/same.test.ts | 6 +- .../inline-multiple-calls/same2.test.ts | 13 + .../test/inline-multiple-calls.test.ts | 224 ++++++++++++++++-- 5 files changed, 239 insertions(+), 23 deletions(-) create mode 100644 test/snapshots/test/fixtures/inline-multiple-calls/different2.test.ts create mode 100644 test/snapshots/test/fixtures/inline-multiple-calls/same2.test.ts diff --git a/test/snapshots/test/fixtures/inline-multiple-calls/different.test.ts b/test/snapshots/test/fixtures/inline-multiple-calls/different.test.ts index 88ae95cd271f..9286f1aefc78 100644 --- a/test/snapshots/test/fixtures/inline-multiple-calls/different.test.ts +++ b/test/snapshots/test/fixtures/inline-multiple-calls/different.test.ts @@ -1,7 +1,7 @@ import { expect, test } from 'vitest' -test('test1', () => { - for (const test1 of ["test1", "test2"]) { - expect(test1).toMatchInlineSnapshot() +test('single', () => { + for (const value of ["test1", "test2"]) { + expect(value).toMatchInlineSnapshot() } }) diff --git a/test/snapshots/test/fixtures/inline-multiple-calls/different2.test.ts b/test/snapshots/test/fixtures/inline-multiple-calls/different2.test.ts new file mode 100644 index 000000000000..76f4b1772f86 --- /dev/null +++ b/test/snapshots/test/fixtures/inline-multiple-calls/different2.test.ts @@ -0,0 +1,13 @@ +import { expect, test } from 'vitest' + +test('a', () => { + snap('test1') +}) + +test('b', () => { + snap('test2') +}) + +function snap(value: unknown) { + expect(value).toMatchInlineSnapshot() +} diff --git a/test/snapshots/test/fixtures/inline-multiple-calls/same.test.ts b/test/snapshots/test/fixtures/inline-multiple-calls/same.test.ts index c4f32383fc23..7d432a1cd6bd 100644 --- a/test/snapshots/test/fixtures/inline-multiple-calls/same.test.ts +++ b/test/snapshots/test/fixtures/inline-multiple-calls/same.test.ts @@ -1,7 +1,7 @@ import { expect, test } from 'vitest' -test('test1', () => { - for (const test1 of ["test1", "test1"]) { - expect(test1).toMatchInlineSnapshot(`"test1"`) +test('single', () => { + for (const value of ["test1", "test1"]) { + expect(value).toMatchInlineSnapshot(`"test1"`) } }) diff --git a/test/snapshots/test/fixtures/inline-multiple-calls/same2.test.ts b/test/snapshots/test/fixtures/inline-multiple-calls/same2.test.ts new file mode 100644 index 000000000000..bbcd666616b6 --- /dev/null +++ b/test/snapshots/test/fixtures/inline-multiple-calls/same2.test.ts @@ -0,0 +1,13 @@ +import { expect, test } from 'vitest' + +test('a', () => { + snap('test1') +}) + +test('b', () => { + snap('test1') +}) + +function snap(value: unknown) { + expect(value).toMatchInlineSnapshot(`"test1"`) +} diff --git a/test/snapshots/test/inline-multiple-calls.test.ts b/test/snapshots/test/inline-multiple-calls.test.ts index 78a70898e3f4..fd8be4f748bb 100644 --- a/test/snapshots/test/inline-multiple-calls.test.ts +++ b/test/snapshots/test/inline-multiple-calls.test.ts @@ -5,8 +5,8 @@ import { editFile, runVitest } from '../../test-utils' // pnpm -C test/snapshots test:snaps inline-multiple-calls -test('same', async () => { - // pnpm -C test/snapshots test:fixtures --root test/fixtures/inline-multiple-calls same +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') @@ -38,7 +38,7 @@ test('same', async () => { "updated": 0, } `) - expect(fs.readFileSync(testFile, 'utf-8')).toContain('expect(test1).toMatchInlineSnapshot(`"test1"`)') + expect(fs.readFileSync(testFile, 'utf-8')).toContain('expect(value).toMatchInlineSnapshot(`"test1"`)') // no-update run vitest = await runVitest({ @@ -65,7 +65,7 @@ test('same', async () => { "updated": 0, } `) - expect(fs.readFileSync(testFile, 'utf-8')).toContain('expect(test1).toMatchInlineSnapshot(`"test1"`)') + expect(fs.readFileSync(testFile, 'utf-8')).toContain('expect(value).toMatchInlineSnapshot(`"test1"`)') // update run vitest = await runVitest({ @@ -91,11 +91,100 @@ test('same', async () => { "updated": 0, } `) - expect(fs.readFileSync(testFile, 'utf-8')).toContain('expect(test1).toMatchInlineSnapshot(`"test1"`)') + expect(fs.readFileSync(testFile, 'utf-8')).toContain('expect(value).toMatchInlineSnapshot(`"test1"`)') }) -test('different', async () => { - // pnpm -C test/snapshots test:fixtures --root test/fixtures/inline-multiple-calls different +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()')) + + // iniital run (create snapshot) + let vitest = await runVitest({ + root, + include: [testFile], + update: true, + }) + expect(vitest.stderr).toBe('') + expect(vitest.ctx?.snapshot.summary).toMatchInlineSnapshot(` + Object { + "added": 2, + "didUpdate": true, + "failure": false, + "filesAdded": 1, + "filesRemoved": 0, + "filesRemovedList": Array [], + "filesUnmatched": 0, + "filesUpdated": 0, + "matched": 0, + "total": 2, + "unchecked": 0, + "uncheckedKeysByFile": Array [], + "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(` + Object { + "added": 0, + "didUpdate": false, + "failure": false, + "filesAdded": 0, + "filesRemoved": 0, + "filesRemovedList": Array [], + "filesUnmatched": 0, + "filesUpdated": 0, + "matched": 2, + "total": 2, + "unchecked": 0, + "uncheckedKeysByFile": Array [], + "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(` + Object { + "added": 0, + "didUpdate": true, + "failure": false, + "filesAdded": 0, + "filesRemoved": 0, + "filesRemovedList": Array [], + "filesUnmatched": 0, + "filesUpdated": 0, + "matched": 2, + "total": 2, + "unchecked": 0, + "uncheckedKeysByFile": Array [], + "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') @@ -114,7 +203,108 @@ Error: toMatchInlineSnapshot with different snapshots cannot be called at the sa Expected: ""test1"" Received: ""test2"" `) - expect(fs.readFileSync(testFile, 'utf-8')).toContain('expect(test1).toMatchInlineSnapshot()') + 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 @@ -125,7 +315,7 @@ Received: ""test2"" }) if (process.env.CI) { expect(vitest.stderr).toContain(` -Error: Snapshot \`test1 1\` mismatched +Error: Snapshot \`a 1\` mismatched `) } else { @@ -136,10 +326,10 @@ Expected: ""test1"" Received: ""test2"" `) } - expect(fs.readFileSync(testFile, 'utf-8')).toContain('expect(test1).toMatchInlineSnapshot()') + expect(fs.readFileSync(testFile, 'utf-8')).toContain('expect(value).toMatchInlineSnapshot()') // current snapshot is "test1" - editFile(testFile, s => s.replace('expect(test1).toMatchInlineSnapshot()', 'expect(test1).toMatchInlineSnapshot(`"test1"`)')) + editFile(testFile, s => s.replace('expect(value).toMatchInlineSnapshot()', 'expect(value).toMatchInlineSnapshot(`"test1"`)')) vitest = await runVitest({ root, include: [testFile], @@ -151,7 +341,7 @@ Error: toMatchInlineSnapshot with different snapshots cannot be called at the sa Expected: ""test1"" Received: ""test2"" `) - expect(fs.readFileSync(testFile, 'utf-8')).toContain('expect(test1).toMatchInlineSnapshot(`"test1"`)') + expect(fs.readFileSync(testFile, 'utf-8')).toContain('expect(value).toMatchInlineSnapshot(`"test1"`)') vitest = await runVitest({ root, @@ -164,10 +354,10 @@ Error: toMatchInlineSnapshot with different snapshots cannot be called at the sa Expected: ""test1"" Received: ""test2"" `) - expect(fs.readFileSync(testFile, 'utf-8')).toContain('expect(test1).toMatchInlineSnapshot(`"test1"`)') + expect(fs.readFileSync(testFile, 'utf-8')).toContain('expect(value).toMatchInlineSnapshot(`"test1"`)') // current snapshot is "test2" - editFile(testFile, s => s.replace('expect(test1).toMatchInlineSnapshot(`"test1"`)', 'expect(test1).toMatchInlineSnapshot(`"test2"`)')) + editFile(testFile, s => s.replace('expect(value).toMatchInlineSnapshot(`"test1"`)', 'expect(value).toMatchInlineSnapshot(`"test2"`)')) vitest = await runVitest({ root, include: [testFile], @@ -179,7 +369,7 @@ Error: toMatchInlineSnapshot with different snapshots cannot be called at the sa Expected: ""test1"" Received: ""test2"" `) - expect(fs.readFileSync(testFile, 'utf-8')).toContain('expect(test1).toMatchInlineSnapshot(`"test2"`)') + expect(fs.readFileSync(testFile, 'utf-8')).toContain('expect(value).toMatchInlineSnapshot(`"test2"`)') vitest = await runVitest({ root, @@ -187,10 +377,10 @@ Received: ""test2"" update: false, }) expect(vitest.stderr).toContain(` -Error: Snapshot \`test1 1\` mismatched +Error: Snapshot \`a 1\` mismatched Expected: ""test2"" Received: ""test1"" `) - expect(fs.readFileSync(testFile, 'utf-8')).toContain('expect(test1).toMatchInlineSnapshot(`"test2"`)') + expect(fs.readFileSync(testFile, 'utf-8')).toContain('expect(value).toMatchInlineSnapshot(`"test2"`)') })