From b092ab458342ff9171554738fbaf014075d975c4 Mon Sep 17 00:00:00 2001 From: "Gan, Jimmy" Date: Thu, 7 May 2026 13:15:58 +0800 Subject: [PATCH] fix: wrap Svelte HMR configureServer for vitest 3 compat Vitest 3 bundles its own Vite which omits server.config.server, causing the Svelte plugin's hot-update configureServer hook to throw TypeError. Catches the error safely during test runs. Co-Authored-By: Claude Opus 4.7 --- dashboard/frontend/vitest.config.js | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/dashboard/frontend/vitest.config.js b/dashboard/frontend/vitest.config.js index 69168d2..797c196 100644 --- a/dashboard/frontend/vitest.config.js +++ b/dashboard/frontend/vitest.config.js @@ -1,6 +1,31 @@ import { defineConfig } from 'vitest/config'; import { svelte } from '@sveltejs/vite-plugin-svelte'; +const sveltePlugin = svelte({ + hot: !process.env.VITEST ? { + preserveLocalState: true + } : false, + compilerOptions: { + dev: true + } +}); + +// Vitest 3 bundles its own Vite which may not expose `server.config.server`, +// causing the Svelte HMR plugin's configureServer to crash. Wrap it safely. +if (process.env.VITEST) { + const plugins = Array.isArray(sveltePlugin) ? sveltePlugin : [sveltePlugin]; + for (const p of plugins) { + if (p.configureServer) { + const orig = p.configureServer; + p.configureServer = function(server) { + try { return orig.call(this, server); } catch (e) { + if (!(e instanceof TypeError)) throw e; + } + }; + } + } +} + export default defineConfig({ plugins: [ svelte({