Add conditional MSW browser worker startup for E2E testing

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
2026-03-22 08:24:33 +08:00
parent 3918755b7c
commit b978071f94
2 changed files with 13 additions and 2 deletions

View File

@@ -5,12 +5,13 @@
"scripts": { "scripts": {
"dev": "vite", "dev": "vite",
"build": "vite build", "build": "vite build",
"build:e2e": "VITE_MSW=true vite build",
"preview": "vite preview", "preview": "vite preview",
"test": "vitest", "test": "vitest",
"coverage": "vitest run --coverage", "coverage": "vitest run --coverage",
"cy:run": "cypress run", "cy:run": "cypress run",
"test:unit": "vitest --environment jsdom", "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'", "test:e2e:dev": "start-server-and-test 'vite dev --port 4173' :4173 'cypress open --e2e'",
"lint": "eslint . --ext .vue,.js,.jsx,.cjs,.mjs", "lint": "eslint . --ext .vue,.js,.jsx,.cjs,.mjs",
"lint:fix": "eslint . --ext .vue,.js,.jsx,.cjs,.mjs --fix", "lint:fix": "eslint . --ext .vue,.js,.jsx,.cjs,.mjs --fix",

View File

@@ -98,4 +98,14 @@ app.component("ContextMenu", ContextMenu);
app.component("Draggable", draggable); // Drag and drop app.component("Draggable", draggable); // Drag and drop
app.directive("tooltip", Tooltip); 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"));