setup.ts 1.0 KB

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