Browse Source

♻️ refactor(shared-test-util): 重构集成测试数据库工具类

- 移除冗余的数据库操作方法(createTestData, findTestData等)
- 优化实体类型导入路径,从shared-utils导入EntityTarget
- 简化initializeWithEntities方法的泛型定义

🔧 chore(shared-test-util): 调整测试配置和生命周期钩子

- 修改vitest配置,将测试文件路径从src/**/*.test.ts改为tests/**/*.test.ts
- 移除setupIntegrationDatabaseHooks方法,统一使用带实体初始化的钩子
- 在测试文件中直接使用setupIntegrationDatabaseHooksWithEntities设置测试环境

♻️ refactor(shared-utils): 导出EntityTarget类型

- 将EntityTarget类型从内部定义改为导出,供其他包使用
- 优化data-source.ts中的类型定义格式

🔧 chore: 添加auth-module包及其依赖配置

- 在pnpm-lock.yaml中添加auth-module的依赖配置
- 添加jsonwebtoken、hono/zod-openapi等必要依赖
- 配置开发环境依赖如@types/jsonwebtoken和vitest覆盖率工具
yourname 4 weeks ago
parent
commit
2c5926f62b

+ 4 - 75
packages/shared-test-util/src/integration-test-db.ts

@@ -1,6 +1,6 @@
-import { DataSource, EntityTarget, ObjectLiteral } from 'typeorm';
+import { DataSource } from 'typeorm';
 import { beforeEach, afterEach } from 'vitest';
 import { beforeEach, afterEach } from 'vitest';
-import { AppDataSource, initializeDataSource } from '@d8d/shared-utils';
+import { AppDataSource, EntityTarget, initializeDataSource } from '@d8d/shared-utils';
 
 
 /**
 /**
  * 集成测试数据库工具类 - 使用真实PostgreSQL数据库
  * 集成测试数据库工具类 - 使用真实PostgreSQL数据库
@@ -29,87 +29,16 @@ export class IntegrationTestDatabase {
   /**
   /**
    * 使用特定实体初始化数据源
    * 使用特定实体初始化数据源
    */
    */
-  static async initializeWithEntities(entities: EntityTarget<ObjectLiteral>[]): Promise<DataSource> {
+  static async initializeWithEntities(entities: EntityTarget[]): Promise<DataSource> {
     initializeDataSource(entities);
     initializeDataSource(entities);
     return await this.getDataSource();
     return await this.getDataSource();
   }
   }
-
-  /**
-   * 清理特定表的数据
-   */
-  static async cleanupTable<T extends ObjectLiteral>(
-    entity: EntityTarget<T>,
-    condition?: Partial<T>
-  ): Promise<void> {
-    const dataSource = await this.getDataSource();
-    const repository = dataSource.getRepository(entity);
-
-    if (condition) {
-      await repository.delete(condition);
-    } else {
-      await repository.clear();
-    }
-  }
-
-  /**
-   * 创建测试数据
-   */
-  static async createTestData<T extends ObjectLiteral>(
-    entity: EntityTarget<T>,
-    data: Partial<T>
-  ): Promise<T> {
-    const dataSource = await this.getDataSource();
-    const repository = dataSource.getRepository(entity);
-
-    const entityInstance = repository.create(data);
-    return await repository.save(entityInstance);
-  }
-
-  /**
-   * 查找测试数据
-   */
-  static async findTestData<T extends ObjectLiteral>(
-    entity: EntityTarget<T>,
-    condition: Partial<T>
-  ): Promise<T | null> {
-    const dataSource = await this.getDataSource();
-    const repository = dataSource.getRepository(entity);
-
-    return await repository.findOne({ where: condition });
-  }
-
-  /**
-   * 删除测试数据
-   */
-  static async deleteTestData<T extends ObjectLiteral>(
-    entity: EntityTarget<T>,
-    condition: Partial<T>
-  ): Promise<void> {
-    const dataSource = await this.getDataSource();
-    const repository = dataSource.getRepository(entity);
-
-    await repository.delete(condition);
-  }
-}
-
-
-/**
- * 集成测试数据库生命周期钩子
- */
-export function setupIntegrationDatabaseHooks() {
-  beforeEach(async () => {
-    await IntegrationTestDatabase.getDataSource();
-  });
-
-  afterEach(async () => {
-    await IntegrationTestDatabase.cleanup();
-  });
 }
 }
 
 
 /**
 /**
  * 集成测试数据库生命周期钩子(带实体初始化)
  * 集成测试数据库生命周期钩子(带实体初始化)
  */
  */
-export function setupIntegrationDatabaseHooksWithEntities(entities: EntityTarget<ObjectLiteral>[]) {
+export function setupIntegrationDatabaseHooksWithEntities(entities: EntityTarget[]) {
   beforeEach(async () => {
   beforeEach(async () => {
     await IntegrationTestDatabase.initializeWithEntities(entities);
     await IntegrationTestDatabase.initializeWithEntities(entities);
   });
   });

+ 6 - 61
packages/shared-test-util/tests/unit/integration-test-db.test.ts

@@ -1,5 +1,5 @@
-import { describe, it, expect, beforeEach, afterEach } from 'vitest';
-import { IntegrationTestDatabase } from '../../src/integration-test-db';
+import { describe, it, expect } from 'vitest';
+import { IntegrationTestDatabase, setupIntegrationDatabaseHooksWithEntities } from '../../src/integration-test-db';
 
 
 // 模拟实体用于测试
 // 模拟实体用于测试
 class MockEntity {
 class MockEntity {
@@ -7,17 +7,10 @@ class MockEntity {
   name?: string;
   name?: string;
 }
 }
 
 
-describe('IntegrationTestDatabase', () => {
-  beforeEach(async () => {
-    // 确保数据库已清理
-    await IntegrationTestDatabase.cleanup();
-  });
-
-  afterEach(async () => {
-    // 清理测试数据库
-    await IntegrationTestDatabase.cleanup();
-  });
+// 设置集成测试钩子 - 这是关键步骤!
+setupIntegrationDatabaseHooksWithEntities([MockEntity])
 
 
+describe('IntegrationTestDatabase', () => {
   describe('getDataSource', () => {
   describe('getDataSource', () => {
     it('应该返回数据源实例', async () => {
     it('应该返回数据源实例', async () => {
       const dataSource = await IntegrationTestDatabase.getDataSource();
       const dataSource = await IntegrationTestDatabase.getDataSource();
@@ -66,53 +59,5 @@ describe('IntegrationTestDatabase', () => {
     });
     });
   });
   });
 
 
-  describe('createTestData', () => {
-    it('应该创建测试数据', async () => {
-      const dataSource = await IntegrationTestDatabase.getDataSource();
-
-      // 注意:这里只是测试方法调用,实际数据库操作需要真实的实体
-      // 在真实环境中,应该使用具体的实体类
-      const testData = { name: 'test' };
-
-      // 验证方法存在且可调用
-      expect(IntegrationTestDatabase.createTestData).toBeDefined();
-      expect(typeof IntegrationTestDatabase.createTestData).toBe('function');
-    });
-  });
-
-  describe('findTestData', () => {
-    it('应该查找测试数据', async () => {
-      // 验证方法存在且可调用
-      expect(IntegrationTestDatabase.findTestData).toBeDefined();
-      expect(typeof IntegrationTestDatabase.findTestData).toBe('function');
-    });
-  });
-
-  describe('deleteTestData', () => {
-    it('应该删除测试数据', async () => {
-      // 验证方法存在且可调用
-      expect(IntegrationTestDatabase.deleteTestData).toBeDefined();
-      expect(typeof IntegrationTestDatabase.deleteTestData).toBe('function');
-    });
-  });
-
-  describe('cleanupTable', () => {
-    it('应该清理表数据', async () => {
-      // 验证方法存在且可调用
-      expect(IntegrationTestDatabase.cleanupTable).toBeDefined();
-      expect(typeof IntegrationTestDatabase.cleanupTable).toBe('function');
-    });
-  });
-});
-
-describe('setupIntegrationDatabaseHooks', () => {
-  it('应该定义生命周期钩子函数', () => {
-    const hooks = require('../../src/integration-test-db');
-
-    expect(hooks.setupIntegrationDatabaseHooks).toBeDefined();
-    expect(typeof hooks.setupIntegrationDatabaseHooks).toBe('function');
-
-    expect(hooks.setupIntegrationDatabaseHooksWithEntities).toBeDefined();
-    expect(typeof hooks.setupIntegrationDatabaseHooksWithEntities).toBe('function');
-  });
+  
 });
 });

+ 1 - 1
packages/shared-test-util/vitest.config.ts

@@ -4,7 +4,7 @@ export default defineConfig({
   test: {
   test: {
     globals: true,
     globals: true,
     environment: 'node',
     environment: 'node',
-    include: ['src/**/*.test.ts'],
+    include: ['tests/**/*.test.ts'],
     coverage: {
     coverage: {
       provider: 'v8',
       provider: 'v8',
       reporter: ['text', 'json', 'html'],
       reporter: ['text', 'json', 'html'],

+ 1 - 1
packages/shared-utils/src/data-source.ts

@@ -3,7 +3,7 @@ import { DataSource, EntitySchema } from "typeorm"
 import process from 'node:process'
 import process from 'node:process'
 
 
 // 实体目标类型,可以是类、函数或实体模式
 // 实体目标类型,可以是类、函数或实体模式
-type EntityTarget = Function | EntitySchema<any> | string
+export type EntityTarget = Function | EntitySchema<any> | string
 
 
 /**
 /**
  * 创建数据源配置
  * 创建数据源配置

+ 71 - 0
pnpm-lock.yaml

@@ -229,6 +229,55 @@ importers:
         specifier: ^0.0.10
         specifier: ^0.0.10
         version: 0.0.10(webpack@5.91.0(@swc/core@1.3.96))
         version: 0.0.10(webpack@5.91.0(@swc/core@1.3.96))
 
 
+  packages/auth-module:
+    dependencies:
+      '@d8d/shared-types':
+        specifier: workspace:*
+        version: link:../shared-types
+      '@d8d/shared-utils':
+        specifier: workspace:*
+        version: link:../shared-utils
+      '@d8d/user-module':
+        specifier: workspace:*
+        version: link:../user-module
+      '@hono/zod-openapi':
+        specifier: 1.0.2
+        version: 1.0.2(hono@4.8.5)(zod@4.1.12)
+      axios:
+        specifier: ^1.12.2
+        version: 1.12.2(debug@4.4.3)
+      debug:
+        specifier: ^4.4.3
+        version: 4.4.3
+      hono:
+        specifier: ^4.8.5
+        version: 4.8.5
+      jsonwebtoken:
+        specifier: ^9.0.2
+        version: 9.0.2
+      typeorm:
+        specifier: ^0.3.20
+        version: 0.3.27(ioredis@5.8.2)(pg@8.16.3)(redis@4.7.1)(reflect-metadata@0.2.2)
+      zod:
+        specifier: ^4.1.12
+        version: 4.1.12
+    devDependencies:
+      '@types/debug':
+        specifier: ^4.1.12
+        version: 4.1.12
+      '@types/jsonwebtoken':
+        specifier: ^9.0.7
+        version: 9.0.10
+      '@vitest/coverage-v8':
+        specifier: ^3.2.4
+        version: 3.2.4(vitest@3.2.4(@types/debug@4.1.12)(@types/node@24.9.1)(happy-dom@18.0.1)(jiti@2.6.1)(jsdom@24.1.3)(lightningcss@1.30.2)(sass@1.93.2)(stylus@0.64.0)(terser@5.44.0)(tsx@4.20.6)(yaml@2.8.1))
+      typescript:
+        specifier: ^5.8.3
+        version: 5.8.3
+      vitest:
+        specifier: ^3.2.4
+        version: 3.2.4(@types/debug@4.1.12)(@types/node@24.9.1)(happy-dom@18.0.1)(jiti@2.6.1)(jsdom@24.1.3)(lightningcss@1.30.2)(sass@1.93.2)(stylus@0.64.0)(terser@5.44.0)(tsx@4.20.6)(yaml@2.8.1)
+
   packages/server:
   packages/server:
     dependencies:
     dependencies:
       '@asteasolutions/zod-to-openapi':
       '@asteasolutions/zod-to-openapi':
@@ -336,6 +385,28 @@ importers:
         specifier: ^3.2.4
         specifier: ^3.2.4
         version: 3.2.4(@types/debug@4.1.12)(@types/node@24.9.1)(happy-dom@18.0.1)(jiti@2.6.1)(jsdom@24.1.3)(lightningcss@1.30.2)(sass@1.93.2)(stylus@0.64.0)(terser@5.44.0)(tsx@4.20.6)(yaml@2.8.1)
         version: 3.2.4(@types/debug@4.1.12)(@types/node@24.9.1)(happy-dom@18.0.1)(jiti@2.6.1)(jsdom@24.1.3)(lightningcss@1.30.2)(sass@1.93.2)(stylus@0.64.0)(terser@5.44.0)(tsx@4.20.6)(yaml@2.8.1)
 
 
+  packages/shared-test-util:
+    dependencies:
+      '@d8d/shared-utils':
+        specifier: workspace:*
+        version: link:../shared-utils
+      hono:
+        specifier: ^4.8.5
+        version: 4.8.5
+      typeorm:
+        specifier: ^0.3.20
+        version: 0.3.27(ioredis@5.8.2)(pg@8.16.3)(redis@4.7.1)(reflect-metadata@0.2.2)
+      vitest:
+        specifier: ^3.2.4
+        version: 3.2.4(@types/debug@4.1.12)(@types/node@24.9.1)(happy-dom@18.0.1)(jiti@2.6.1)(jsdom@24.1.3)(lightningcss@1.30.2)(sass@1.93.2)(stylus@0.64.0)(terser@5.44.0)(tsx@4.20.6)(yaml@2.8.1)
+    devDependencies:
+      '@vitest/coverage-v8':
+        specifier: ^3.2.4
+        version: 3.2.4(vitest@3.2.4(@types/debug@4.1.12)(@types/node@24.9.1)(happy-dom@18.0.1)(jiti@2.6.1)(jsdom@24.1.3)(lightningcss@1.30.2)(sass@1.93.2)(stylus@0.64.0)(terser@5.44.0)(tsx@4.20.6)(yaml@2.8.1))
+      typescript:
+        specifier: ^5.8.3
+        version: 5.8.3
+
   packages/shared-types:
   packages/shared-types:
     devDependencies:
     devDependencies:
       typescript:
       typescript: