Sfoglia il codice sorgente

♻️ refactor(test): 优化集成测试配置

- 移除test:integration命令中多余的客户端测试路径
- 集成测试工具直接使用主API应用实例替代手动创建
- 用户API测试中移除显式路由注册,使用全局API配置

✅ test(server): 改进测试环境设置

- 简化测试环境初始化流程,减少重复代码
- 确保测试使用与生产环境一致的API配置
- 提高测试稳定性和与实际应用的一致性
yourname 2 mesi fa
parent
commit
3d05e3ae28

+ 1 - 1
package.json

@@ -18,7 +18,7 @@
     "test:api:coverage": "vitest --coverage src/server/**/__tests__/** src/server/**/__integration_tests__/**",
     "test:integration:coverage": "vitest --coverage src/server/**/__tests__/*.integration.test.ts src/server/__integration_tests__/**",
     "test:components:coverage": "vitest --coverage --config=vitest.config.components.ts",
-    "test:integration": "vitest src/server/**/__tests__/*.integration.test.ts src/server/__integration_tests__/** src/client/__integration_tests__/**",
+    "test:integration": "vitest src/server/**/__tests__/*.integration.test.ts src/server/__integration_tests__/**",
     "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",

+ 3 - 10
src/server/__test_utils__/integration-test-utils.ts

@@ -4,6 +4,7 @@ import { IntegrationTestDatabase } from './integration-test-db';
 import { DataSource } from 'typeorm';
 import { UserEntity } from '../modules/users/user.entity';
 import { Role } from '../modules/users/role.entity';
+import apiApp from '../api';
 
 /**
  * 集成测试配置选项
@@ -30,16 +31,8 @@ export async function createIntegrationTestApp(
   routes: any[],
   options: IntegrationTestOptions = {}
 ): Promise<OpenAPIHono> {
-  const app = new OpenAPIHono();
-
-  // 注册所有路由
-  routes.forEach(route => {
-    if (typeof route === 'function') {
-      route(app);
-    }
-  });
-
-  return app;
+  // 使用主API应用
+  return apiApp;
 }
 
 /**

+ 1 - 2
src/server/api/users/__tests__/users.integration.test.ts

@@ -1,5 +1,4 @@
 import { describe, it, expect, beforeEach, afterEach } from 'vitest';
-import userRoutes from '../index';
 import {
   setupIntegrationTestHooks,
   TestDataFactory,
@@ -15,7 +14,7 @@ describe('用户API集成测试', () => {
 
   beforeEach(async () => {
     // 设置测试环境
-    testContext = await setupIntegrationTestEnvironment([userRoutes], {
+    testContext = await setupIntegrationTestEnvironment([], {
       setupDatabase: true,
       setupAuth: true
     });