Sfoglia il codice sorgente

♻️ refactor(test): migrate to shared test utilities

- add @d8d/shared-test-util dependency to package.json
- replace local integration test database hooks with shared setupIntegrationDatabaseHooksWithEntities
- remove redundant IntegrationTestDatabase class from test utilities
- update imports to use shared test utilities

✅ test(integration): update test setup with entity configuration

- pass UserEntity and Role to shared database setup hook
- maintain test data factory functionality while using shared utilities
yourname 3 settimane fa
parent
commit
267844c0d0

+ 1 - 0
packages/user-module/package.json

@@ -41,6 +41,7 @@
     "@d8d/shared-crud": "workspace:*",
     "@d8d/shared-types": "workspace:*",
     "@d8d/shared-utils": "workspace:*",
+    "@d8d/shared-test-util": "workspace:*",
     "@hono/zod-openapi": "1.0.2",
     "bcrypt": "^6.0.0",
     "hono": "^4.8.5",

+ 9 - 5
packages/user-module/tests/integration/user.routes.integration.test.ts

@@ -2,14 +2,18 @@ import { describe, it, expect, beforeEach } from 'vitest';
 import { testClient } from 'hono/testing';
 import {
   IntegrationTestDatabase,
-  setupIntegrationDatabaseHooks,
-  TestDataFactory
-} from '../utils/integration-test-db';
-import { IntegrationTestAssertions } from '../utils/integration-test-utils';
+  setupIntegrationDatabaseHooksWithEntities
+} from '@d8d/shared-test-util';
+import {
+  IntegrationTestAssertions
+} from '../utils/integration-test-utils';
 import { userRoutes } from '../../src/routes';
+import { UserEntity } from '../../src/entities/user.entity';
+import { Role } from '../../src/entities/role.entity';
+import { TestDataFactory } from '../utils/integration-test-db';
 
 // 设置集成测试钩子
-setupIntegrationDatabaseHooks()
+setupIntegrationDatabaseHooksWithEntities([UserEntity, Role])
 
 describe('用户路由API集成测试 (使用hono/testing)', () => {
   let client: ReturnType<typeof testClient<typeof userRoutes>>;

+ 0 - 43
packages/user-module/tests/utils/integration-test-db.ts

@@ -1,36 +1,6 @@
 import { DataSource } from 'typeorm';
-import { beforeEach, afterEach } from 'vitest';
 import { UserEntity } from '../../src/entities/user.entity';
 import { Role } from '../../src/entities/role.entity';
-import { AppDataSource, initializeDataSource } from '@d8d/shared-utils';
-
-/**
- * 集成测试数据库工具类 - 使用真实PostgreSQL数据库
- */
-export class IntegrationTestDatabase {
-  /**
-   * 清理集成测试数据库
-   */
-  static async cleanup(): Promise<void> {
-    if (AppDataSource.isInitialized) {
-      await AppDataSource.destroy();
-    }
-  }
-
-  /**
-   * 获取当前数据源
-   */
-  static async getDataSource(): Promise<DataSource> {
-    if (!AppDataSource) {
-      initializeDataSource([UserEntity, Role]);
-    }
-
-    if (!AppDataSource.isInitialized) {
-      await AppDataSource.initialize();
-    }
-    return AppDataSource;
-  }
-}
 
 /**
  * 测试数据工厂类
@@ -87,17 +57,4 @@ export class TestDataFactory {
     const role = roleRepository.create(roleData);
     return await roleRepository.save(role);
   }
-}
-
-/**
- * 集成测试数据库生命周期钩子
- */
-export function setupIntegrationDatabaseHooks() {
-  beforeEach(async () => {
-    await IntegrationTestDatabase.getDataSource();
-  });
-
-  afterEach(async () => {
-    await IntegrationTestDatabase.cleanup();
-  });
 }

+ 1 - 1
packages/user-module/tests/utils/integration-test-utils.ts

@@ -1,4 +1,4 @@
-import { IntegrationTestDatabase } from './integration-test-db';
+import { IntegrationTestDatabase } from '@d8d/shared-test-util';
 import { UserEntity } from '../../src/entities/user.entity';
 
 /**

+ 3 - 0
pnpm-lock.yaml

@@ -470,6 +470,9 @@ importers:
       '@d8d/shared-crud':
         specifier: workspace:*
         version: link:../shared-crud
+      '@d8d/shared-test-util':
+        specifier: workspace:*
+        version: link:../shared-test-util
       '@d8d/shared-types':
         specifier: workspace:*
         version: link:../shared-types