setup.ts 1.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. // 测试环境全局设置
  2. import { jest } from '@jest/globals';
  3. // 全局测试超时设置
  4. jest.setTimeout(10000);
  5. // 全局测试前置处理
  6. beforeAll(() => {
  7. // 设置测试环境变量
  8. process.env.NODE_ENV = 'test';
  9. // 抑制控制台输出(测试中)
  10. jest.spyOn(console, 'log').mockImplementation(() => {});
  11. jest.spyOn(console, 'error').mockImplementation(() => {});
  12. jest.spyOn(console, 'warn').mockImplementation(() => {});
  13. jest.spyOn(console, 'info').mockImplementation(() => {});
  14. });
  15. // 全局测试后置清理
  16. afterAll(() => {
  17. // 恢复控制台输出
  18. jest.restoreAllMocks();
  19. });
  20. // 每个测试后的清理
  21. afterEach(() => {
  22. jest.clearAllMocks();
  23. });
  24. // 全局测试工具函数
  25. globalThis.createTestContext = () => ({
  26. timestamp: new Date().toISOString(),
  27. requestId: `test_${Math.random().toString(36).substr(2, 9)}`
  28. });
  29. // 类型声明
  30. declare global {
  31. // eslint-disable-next-line no-var
  32. var createTestContext: () => {
  33. timestamp: string;
  34. requestId: string;
  35. };
  36. }
  37. export {};