From b978071f94ecb766d53f97a1fbd32e5c189c1da8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=BE=9D=E7=91=AA=E8=B2=93?= Date: Sun, 22 Mar 2026 08:24:33 +0800 Subject: [PATCH] Add conditional MSW browser worker startup for E2E testing Co-Authored-By: Claude Opus 4.6 --- package.json | 3 ++- src/main.ts | 12 +++++++++++- 2 files changed, 13 insertions(+), 2 deletions(-) diff --git a/package.json b/package.json index 811502d..10048c5 100644 --- a/package.json +++ b/package.json @@ -5,12 +5,13 @@ "scripts": { "dev": "vite", "build": "vite build", + "build:e2e": "VITE_MSW=true vite build", "preview": "vite preview", "test": "vitest", "coverage": "vitest run --coverage", "cy:run": "cypress run", "test:unit": "vitest --environment jsdom", - "test:e2e": "start-server-and-test preview :4173 'cypress run --e2e'", + "test:e2e": "start-server-and-test 'vite preview --port 4173' :4173 'cypress run --e2e'", "test:e2e:dev": "start-server-and-test 'vite dev --port 4173' :4173 'cypress open --e2e'", "lint": "eslint . --ext .vue,.js,.jsx,.cjs,.mjs", "lint:fix": "eslint . --ext .vue,.js,.jsx,.cjs,.mjs --fix", diff --git a/src/main.ts b/src/main.ts index 60efcc5..d762eb6 100644 --- a/src/main.ts +++ b/src/main.ts @@ -98,4 +98,14 @@ app.component("ContextMenu", ContextMenu); app.component("Draggable", draggable); // Drag and drop app.directive("tooltip", Tooltip); -app.mount("#app"); +/** + * Starts the MSW service worker when VITE_MSW is set. + * Used for E2E testing with mock API responses. + */ +async function enableMocking() { + if (import.meta.env.VITE_MSW !== "true") return; + const { worker } = await import("./mocks/browser.js"); + await worker.start({ onUnhandledRequest: "bypass" }); +} + +enableMocking().then(() => app.mount("#app"));