Skip to content

Commit

Permalink
fix(snapshot): reject multiple toMatchInlineSnapshot updates at the…
Browse files Browse the repository at this point in the history
… same location (#6332)
  • Loading branch information
hi-ogawa committed Aug 13, 2024
1 parent 153ff01 commit 1606f34
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 1 deletion.
7 changes: 7 additions & 0 deletions packages/snapshot/src/port/state.ts
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,13 @@ export default class SnapshotState {
// location for js files, but `column-1` points to the same in both js/ts
// https://github.com/vitejs/vite/issues/8657
stack.column--
// reject multiple inline snapshots at the same location
const duplicateIndex = this._inlineSnapshots.findIndex(s => s.file === stack.file && s.line === stack.line && s.column === stack.column)
if (duplicateIndex >= 0) {
// remove the first one to avoid updating an inline snapshot
this._inlineSnapshots.splice(duplicateIndex, 1)
throw new Error('toMatchInlineSnapshot cannot be called multiple times at the same location.')
}
this._inlineSnapshots.push({
snapshot: receivedSerialized,
...stack,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import {test, expect} from "vitest";

test("ok", () => {
expect("ok").toMatchInlineSnapshot(`"ok"`);
});

test("fail 1", () => {
for (const str of ["foo", "bar"]) {
expect(str).toMatchInlineSnapshot();
}
});

test("fail 3", () => {
for (const str of ["ok", "ok"]) {
expect(str).toMatchInlineSnapshot();
}
});

test("somehow ok", () => {
for (const str of ["ok", "ok"]) {
expect(str).toMatchInlineSnapshot(`"ok"`);
}
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import {test, expect} from "vitest";

// this test causes infinite re-run when --watch and --update
// since snapshot update switches between "foo" and "bar" forever.
test("fail 2", () => {
for (const str of ["foo", "bar"]) {
expect(str).toMatchInlineSnapshot(`"bar"`);
}
});
7 changes: 7 additions & 0 deletions test/cli/test/__snapshots__/fails.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,13 @@ 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-update-all.test.ts > inline-snapshop-inside-loop-update-all.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."
`;
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"`;
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."`;
exports[`should fail nested-suite.test.ts > nested-suite.test.ts 1`] = `"AssertionError: expected true to be false // Object.is equality"`;
Expand Down
5 changes: 4 additions & 1 deletion test/cli/test/fails.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,10 @@ const root = resolve(__dirname, '../fixtures/fails')
const files = await fg('**/*.test.ts', { cwd: root, dot: true })

it.each(files)('should fail %s', async (file) => {
const { stderr } = await runVitest({ root }, [file])
const { stderr } = await runVitest({
root,
update: file === 'inline-snapshop-inside-loop-update-all.test.ts' ? true : undefined,
}, [file])

expect(stderr).toBeTruthy()
const msg = String(stderr)
Expand Down

0 comments on commit 1606f34

Please sign in to comment.