Browse Source

✅ test(auth): 优化认证模块集成测试

- 在测试中添加File实体到数据库设置,确保完整的实体关系
- 统一使用IntegrationTestDatabase获取数据源,提高测试一致性
- 添加afterEach导入,完善测试生命周期管理
- 确保ErrorSchema正确导出,修复潜在的测试错误
yourname 3 weeks ago
parent
commit
16a1239397

+ 2 - 1
packages/auth-module/tests/integration/auth.integration.test.ts

@@ -5,6 +5,7 @@ import {
   setupIntegrationDatabaseHooksWithEntities,
 } from '@d8d/shared-test-util';
 import { Role, UserEntity } from '@d8d/user-module';
+import { File } from '@d8d/file-module';
 import authRoutes from '../../src/routes';
 import { AuthService } from '../../src/services';
 import { UserService } from '@d8d/user-module';
@@ -12,7 +13,7 @@ import { DisabledStatus } from '@d8d/shared-types';
 import { TestDataFactory } from '../utils/test-data-factory';
 
 // 设置集成测试钩子
-setupIntegrationDatabaseHooksWithEntities([UserEntity, Role])
+setupIntegrationDatabaseHooksWithEntities([UserEntity, Role, File])
 
 describe('认证API集成测试 (使用hono/testing)', () => {
   let client: ReturnType<typeof testClient<typeof authRoutes>>;

+ 14 - 5
packages/auth-module/tests/integration/phone-decrypt.integration.test.ts

@@ -1,8 +1,9 @@
-import { describe, it, expect, beforeEach, vi } from 'vitest';
+import { describe, it, expect, beforeEach, vi, afterEach } from 'vitest';
 import { testClient } from 'hono/testing';
 import { authRoutes } from '../../src/routes';
-import { AppDataSource } from '@d8d/shared-utils';
+import { IntegrationTestDatabase, setupIntegrationDatabaseHooksWithEntities } from '@d8d/shared-test-util';
 import { UserEntity } from '@d8d/user-module';
+import { File } from '@d8d/file-module';
 
 // Mock MiniAuthService 的 decryptPhoneNumber 方法
 vi.mock('../../src/services/mini-auth.service', () => ({
@@ -32,10 +33,14 @@ vi.mock('@d8d/shared-utils', async (importOriginal) => {
     ...actual,
     redisUtil: {
       getSessionKey: vi.fn().mockResolvedValue('mock-session-key')
-    }
+    },
+    ErrorSchema: actual.ErrorSchema // 确保ErrorSchema被正确导出
   };
 });
 
+// 设置集成测试钩子
+setupIntegrationDatabaseHooksWithEntities([UserEntity, File])
+
 describe('手机号解密API集成测试', () => {
   let client: ReturnType<typeof testClient<typeof authRoutes>>;
   let testToken: string;
@@ -45,8 +50,11 @@ describe('手机号解密API集成测试', () => {
     // 创建测试客户端
     client = testClient(authRoutes);
 
+    // 获取数据源
+    const dataSource = await IntegrationTestDatabase.getDataSource();
+
     // 创建测试用户
-    const userRepository = AppDataSource.getRepository(UserEntity);
+    const userRepository = dataSource.getRepository(UserEntity);
     testUser = userRepository.create({
       username: `test_user_${Date.now()}`,
       password: 'test_password',
@@ -91,7 +99,8 @@ describe('手机号解密API集成测试', () => {
       }
 
       // 验证数据库中的用户手机号已更新
-      const userRepository = AppDataSource.getRepository(UserEntity);
+      const dataSource = await IntegrationTestDatabase.getDataSource();
+      const userRepository = dataSource.getRepository(UserEntity);
       const updatedUser = await userRepository.findOne({
         where: { id: testUser.id }
       });