simple-test.test.ts 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. import { describe, it, expect, beforeEach } from 'vitest';
  2. import { testClient } from 'hono/testing';
  3. import { IntegrationTestDatabase, setupIntegrationDatabaseHooksWithEntities } from '@d8d/shared-test-util';
  4. import { JWTUtil } from '@d8d/shared-utils';
  5. import { UserEntityMt, RoleMt } from '@d8d/user-module-mt';
  6. import { FileMt } from '@d8d/file-module-mt';
  7. import userOrderRoutes from '../../src/routes/user/orders.mt';
  8. import { OrdersTestFactory } from '../factories/orders-test-factory';
  9. // 设置集成测试钩子
  10. setupIntegrationDatabaseHooksWithEntities([
  11. UserEntityMt,
  12. RoleMt,
  13. FileMt
  14. ])
  15. describe('简单测试', () => {
  16. let client: ReturnType<typeof testClient<typeof userOrderRoutes>>;
  17. let testFactory: OrdersTestFactory;
  18. let userToken: string;
  19. beforeEach(async () => {
  20. // 创建测试客户端
  21. client = testClient(userOrderRoutes);
  22. // 获取数据源并创建测试工厂
  23. const dataSource = await IntegrationTestDatabase.getDataSource();
  24. testFactory = new OrdersTestFactory(dataSource);
  25. // 创建测试用户
  26. const testUser = await testFactory.createTestUser(1);
  27. // 生成JWT令牌
  28. userToken = JWTUtil.generateToken({ id: testUser.id, username: testUser.username, tenantId: 1 });
  29. });
  30. it('应该返回空订单列表', async () => {
  31. // 使用更简单的调用方式,避免查询参数问题
  32. const response = await fetch('http://localhost:3000/orders', {
  33. method: 'GET',
  34. headers: {
  35. 'Authorization': `Bearer ${userToken}`,
  36. 'Content-Type': 'application/json'
  37. }
  38. });
  39. console.debug('Simple test response status:', response.status);
  40. if (response.status !== 200) {
  41. const errorData = await response.json();
  42. console.debug('Simple test error response:', errorData);
  43. }
  44. expect(response.status).toBe(200);
  45. });
  46. });