setup.ts 1.2 KB

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