From bfe4e09b7ee0d9d9e258b0207d9db4850e5f225c Mon Sep 17 00:00:00 2001 From: Molecule AI Frontend Engineer Date: Fri, 17 Apr 2026 06:09:39 +0000 Subject: [PATCH] fix(canvas): move vi.mock to module top level in ZoomShortcut.test (#632) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The vi.mock("../../../store/canvas") call was nested inside an it() block. Vitest hoists all vi.mock calls to module scope at runtime regardless, so the code never matched its actual execution order — prompting the "not at top level" warning that Vitest will make a hard error in a future version. Move the mock to after the imports, remove the now-redundant inline call from the it() body, and add a comment explaining the hoisting rule. Co-Authored-By: Claude Sonnet 4.6 --- .../__tests__/ZoomShortcut.test.tsx | 34 ++++++++++--------- 1 file changed, 18 insertions(+), 16 deletions(-) diff --git a/canvas/src/components/__tests__/ZoomShortcut.test.tsx b/canvas/src/components/__tests__/ZoomShortcut.test.tsx index b1521fe6..6b227c0f 100644 --- a/canvas/src/components/__tests__/ZoomShortcut.test.tsx +++ b/canvas/src/components/__tests__/ZoomShortcut.test.tsx @@ -6,6 +6,24 @@ import React from "react"; import { render, screen, fireEvent, cleanup } from "@testing-library/react"; import { afterEach, beforeEach, describe, expect, it, vi } from "vitest"; +// vi.mock is hoisted to module top level by Vitest regardless of where it appears +// in the source. Placing it here explicitly matches that runtime behaviour and +// silences the "not at top level" warning (closes #632). +vi.mock("../../../store/canvas", () => ({ + useCanvasStore: Object.assign( + vi.fn(() => null), + { + getState: () => ({ + selectedNodeId: null, + nodes: [], + contextMenu: null, + closeContextMenu: vi.fn(), + selectNode: vi.fn(), + }), + } + ), +})); + afterEach(() => cleanup()); // ─── Z key handler unit tests (no React needed) ───────────────────────────── @@ -25,22 +43,6 @@ describe("Z key → molecule:zoom-to-team", () => { }); it("does NOT fire when no node is selected", () => { - // Simulate store: no selection - vi.mock("../../../store/canvas", () => ({ - useCanvasStore: Object.assign( - vi.fn(() => null), - { - getState: () => ({ - selectedNodeId: null, - nodes: [], - contextMenu: null, - closeContextMenu: vi.fn(), - selectNode: vi.fn(), - }), - } - ), - })); - fireEvent.keyDown(window, { key: "Z" }); expect(dispatchedEvents).toHaveLength(0); });