Browse Source

✅ test(config): 重构测试配置并统一vitest项目设置

- 合并vitest配置文件,使用projects配置区分node和happy-dom环境
- 更新package.json中的test脚本,使用--project参数指定测试环境
- 删除单独的vitest.config.components.ts配置文件
- 调整测试覆盖率阈值,从70%调整为65%
- 在claude设置中添加API和组件测试命令

🔧 chore(scripts): 优化测试相关npm脚本

- 简化test和test:coverage脚本
- 为test:api和test:components添加--project参数
- 统一测试命令格式,使用一致的命名规范
yourname 1 month ago
parent
commit
d56b4c201e
4 changed files with 79 additions and 143 deletions
  1. 3 1
      .claude/settings.local.json
  2. 7 7
      package.json
  3. 0 86
      vitest.config.components.ts
  4. 69 49
      vitest.config.ts

+ 3 - 1
.claude/settings.local.json

@@ -29,7 +29,9 @@
       "Bash(pnpm run db:backup:latest:*)",
       "Bash(done)",
       "Bash(do sed -i '8d' \"$file\")",
-      "Bash(pnpm typecheck)"
+      "Bash(pnpm typecheck)",
+      "Bash(pnpm test:api:*)",
+      "Bash(pnpm test:components:*)"
     ],
     "deny": [],
     "ask": []

+ 7 - 7
package.json

@@ -9,13 +9,13 @@
     "build:client": "vite build --outDir dist/client --manifest",
     "build:server": "vite build --ssr src/server/index.tsx --outDir dist/server",
     "start": "PORT=8080 cross-env NODE_ENV=production node server",
-    "test": "npm run test:api",
-    "test:coverage": "npm run test:api:coverage && npm run test:components:coverage",
-    "test:api": "vitest",
-    "test:components": "vitest --config=vitest.config.components.ts",
-    "test:integration": "npm run test:components",
-    "test:api:coverage": "vitest --coverage",
-    "test:components:coverage": "vitest --coverage --config=vitest.config.components.ts",
+    "test": "vitest",
+    "test:coverage": "vitest --coverage",
+    "test:api": "vitest run --project=node",
+    "test:components": "vitest run --project=happy-dom",
+    "test:integration": "vitest run --project=happy-dom",
+    "test:api:coverage": "vitest run --coverage --project=node",
+    "test:components:coverage": "vitest run --coverage --project=happy-dom",
     "test:e2e": "playwright test --config=tests/e2e/playwright.config.ts",
     "test:e2e:ui": "playwright test --config=tests/e2e/playwright.config.ts --ui",
     "test:e2e:debug": "playwright test --config=tests/e2e/playwright.config.ts --debug",

+ 0 - 86
vitest.config.components.ts

@@ -1,86 +0,0 @@
-import { defineConfig } from 'vitest/config'
-import { resolve } from 'path'
-
-export default defineConfig({
-  test: {
-    // 测试环境 - 使用happy-dom进行组件测试
-    environment: 'happy-dom',
-
-    // 测试文件匹配模式
-    include: [
-      'src/client/__integration_tests__/**/*.test.{js,ts,jsx,tsx}',
-      'src/client/__tests__/**/*.test.{js,ts,jsx,tsx}',
-      'src/client/**/__tests__/**/*.test.{js,ts,jsx,tsx}',
-      'src/client/**/*.test.{js,ts,jsx,tsx}'
-    ],
-
-    // 排除模式
-    exclude: [
-      '**/node_modules/**',
-      '**/dist/**',
-      '**/build/**',
-      '**/coverage/**',
-      'tests/e2e/**',   // 排除e2e测试代码
-      'src/server/**',
-      'src/client/home/**',
-      'src/client/components/ui/**',
-      'src/client/__test_utils__/**',
-    ],
-
-    // 覆盖率配置
-    coverage: {
-      provider: 'v8',
-      reporter: ['text', 'lcov', 'html'],
-      reportsDirectory: './coverage/components',
-      exclude: [
-        '**/node_modules/**',
-        '**/dist/**',
-        '**/build/**',
-        '**/coverage/**',
-        '**/*.d.ts',
-        'src/client/api.ts',
-        'tests/e2e/**',   // 排除e2e测试代码
-        'scripts/**',     // 排除脚本目录
-        'src/test/**',    // 排除测试工具目录
-        '**/__tests__/**',
-        '**/__mocks__/**',
-        '**/index.ts',
-        '**/types.ts',
-        'src/server/**',
-        'src/client/home/**',
-        'src/client/components/ui/**',
-        'src/client/__test_utils__/**',
-        'vitest.config.ts',
-        'vitest.config.components.ts',
-        'vite.config.ts',
-        'server.js',
-        'eslint.config.js',
-        'debug-page.js',
-      ],
-      thresholds: {
-        branches: 60,
-        functions: 60,
-        lines: 60,
-        statements: 60
-      }
-    },
-
-    // 全局设置
-    globals: true,
-
-    // 测试超时
-    testTimeout: 10000,
-
-    // 设置文件
-    setupFiles: ['./src/test/setup.ts'],
-
-    // 别名配置
-    alias: {
-      '@': resolve(__dirname, './src'),
-      '@/client': resolve(__dirname, './src/client'),
-      '@/server': resolve(__dirname, './src/server'),
-      '@/share': resolve(__dirname, './src/share'),
-      '@/test': resolve(__dirname, './test')
-    }
-  }
-})

+ 69 - 49
vitest.config.ts

@@ -3,28 +3,68 @@ import { resolve } from 'path'
 
 export default defineConfig({
   test: {
-    // 测试环境
-    environment: 'node',
+    projects: [
+      // Node.js 环境项目 - 后端测试
+      {
+        test: {
+          name: 'node',
+          environment: 'node',
+          include: [
+            'src/server/**/*.test.{ts,js}',
+            'src/server/**/*.integration.test.{ts,js}'
+          ],
+          exclude: [
+            '**/node_modules/**',
+            '**/dist/**',
+            '**/build/**',
+            '**/coverage/**',
+            'tests/e2e/**'
+          ],
 
-    // 测试文件匹配模式
-    include: [
-      'src/server/api/**/__tests__/**',
-      'src/server/modules/**/__tests__/**',
-      'src/server/utils/**/__tests__/**',
-      'src/server/**/__integration_tests__/**'
-    ],
-
-    // 排除模式
-    exclude: [
-      '**/node_modules/**',
-      '**/dist/**',
-      '**/build/**',
-      '**/coverage/**',
-      'tests/e2e/**',  // 排除Playwright E2E测试文件
-      'src/client/**',  // 排除客户端代码,由组件测试配置处理
+          alias: {
+            '@': resolve(__dirname, './src'),
+            '@/client': resolve(__dirname, './src/client'),
+            '@/server': resolve(__dirname, './src/server'),
+            '@/share': resolve(__dirname, './src/share'),
+            '@/test': resolve(__dirname, './src/test')
+          }
+        }
+      },
+      // Happy DOM 环境项目 - 前端组件测试
+      {
+        test: {
+          name: 'happy-dom',
+          environment: 'happy-dom',
+          include: [
+            'src/client/**/*.test.{ts,js,tsx,jsx}',
+            'src/client/**/*.integration.test.{ts,js,tsx,jsx}'
+          ],
+          exclude: [
+            '**/node_modules/**',
+            '**/dist/**',
+            '**/build/**',
+            '**/coverage/**',
+            'tests/e2e/**',
+            'src/client/home/**',
+            'src/client/components/ui/**',
+            'src/client/__test_utils__/**',
+          ],
+          alias: {
+            '@': resolve(__dirname, './src'),
+            '@/client': resolve(__dirname, './src/client'),
+            '@/server': resolve(__dirname, './src/server'),
+            '@/share': resolve(__dirname, './src/share'),
+            '@/test': resolve(__dirname, './src/test')
+          },
+        },
+      }
     ],
 
-    // 覆盖率配置
+    // 共享配置
+    globals: true,
+    testTimeout: 10000,
+    setupFiles: ['./src/test/setup.ts'],
+    // 覆盖率配置 - 放在根级别
     coverage: {
       provider: 'v8',
       reporter: ['text', 'lcov', 'html'],
@@ -35,10 +75,9 @@ export default defineConfig({
         '**/build/**',
         '**/coverage/**',
         '**/*.d.ts',
-        'src/client/**',  // 排除所有客户端代码
-        'tests/e2e/**',   // 排除e2e测试代码
-        'scripts/**',     // 排除脚本目录
-        'src/test/**',    // 排除测试工具目录
+        'tests/e2e/**',
+        'scripts/**',
+        'src/test/**',
         '**/__tests__/**',
         '**/__mocks__/**',
         '**/index.ts',
@@ -50,34 +89,15 @@ export default defineConfig({
         'eslint.config.js',
         'debug-page.js',
         'src/server/__test_utils__/**',
+        'src/client/__test_utils__/**',
       ],
       thresholds: {
-        branches: 70,
-        functions: 70,
-        lines: 70,
-        statements: 70
+        branches: 65,
+        functions: 65,
+        lines: 65,
+        statements: 65
       }
-    },
-
-    // 全局设置
-    globals: true,
-
-    // 测试超时
-    testTimeout: 10000,
-
-    // 设置文件
-    setupFiles: ['./src/test/setup.ts'],
-
-    // 别名配置
-    alias: {
-      '@': resolve(__dirname, './src'),
-      '@/client': resolve(__dirname, './src/client'),
-      '@/server': resolve(__dirname, './src/server'),
-      '@/share': resolve(__dirname, './src/share'),
-      '@/test': resolve(__dirname, './test')
-    },
+    }
+  },
 
-    // api测试关闭并行测试
-    fileParallelism: false
-  }
 })