From 0e9d4ae43e40d9e7b16f643bf9bc4315486fd41b Mon Sep 17 00:00:00 2001 From: kurihada Date: Sun, 1 Mar 2026 21:51:14 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E4=BF=AE=E5=A4=8D=20Vitest=204.x=20?= =?UTF-8?q?=E6=9E=84=E5=BB=BA=E5=85=BC=E5=AE=B9=E6=80=A7=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit environmentMatchGlobs 在 Vitest 4 中已移除,改用 projects 配置; 排除 vitest.config.ts 不参与 Next.js 类型检查。 --- tsconfig.json | 2 +- vitest.config.ts | 40 ++++++++++++++++++++++++++++++---------- 2 files changed, 31 insertions(+), 11 deletions(-) diff --git a/tsconfig.json b/tsconfig.json index cf9c65d..11f74a2 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -30,5 +30,5 @@ ".next/dev/types/**/*.ts", "**/*.mts" ], - "exclude": ["node_modules"] + "exclude": ["node_modules", "vitest.config.ts"] } diff --git a/vitest.config.ts b/vitest.config.ts index 6094f2a..09c011a 100644 --- a/vitest.config.ts +++ b/vitest.config.ts @@ -2,6 +2,15 @@ import { defineConfig } from "vitest/config"; import react from "@vitejs/plugin-react"; import path from "path"; +const shared = { + globals: true, + setupFiles: ["src/__tests__/helpers/setup.ts"], + env: { + AMAP_API_KEY: "test-amap-key", + DEEPSEEK_API_KEY: "test-deepseek-key", + }, +} as const; + export default defineConfig({ plugins: [react()], resolve: { @@ -10,16 +19,27 @@ export default defineConfig({ }, }, test: { - globals: true, - environment: "jsdom", - setupFiles: ["src/__tests__/helpers/setup.ts"], - environmentMatchGlobs: [ - ["src/app/api/**/*.test.ts", "node"], - ["src/lib/**/*.test.ts", "node"], + projects: [ + { + plugins: [react()], + resolve: { alias: { "@": path.resolve(__dirname, "src") } }, + test: { + ...shared, + name: "ui", + environment: "jsdom", + include: ["src/**/*.test.{ts,tsx}"], + exclude: ["src/app/api/**", "src/lib/**/*.test.ts"], + }, + }, + { + resolve: { alias: { "@": path.resolve(__dirname, "src") } }, + test: { + ...shared, + name: "api", + environment: "node", + include: ["src/app/api/**/*.test.ts", "src/lib/**/*.test.ts"], + }, + }, ], - env: { - AMAP_API_KEY: "test-amap-key", - DEEPSEEK_API_KEY: "test-deepseek-key", - }, }, });