| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748 |
- // 测试环境全局设置
- import { beforeAll, afterAll, afterEach, vi, expect } from 'vitest';
- import * as matchers from '@testing-library/jest-dom/matchers';
- // 全局测试超时设置已在 vitest.config.ts 中配置
- // 扩展expect匹配器
- expect.extend(matchers);
- // 全局测试前置处理
- beforeAll(() => {
- // 设置测试环境变量
- process.env.NODE_ENV = 'test';
- // 抑制控制台输出(测试中)
- vi.spyOn(console, 'log').mockImplementation(() => {});
- vi.spyOn(console, 'error').mockImplementation(() => {});
- vi.spyOn(console, 'warn').mockImplementation(() => {});
- vi.spyOn(console, 'info').mockImplementation(() => {});
- });
- // 全局测试后置清理
- afterAll(() => {
- // 恢复控制台输出
- vi.restoreAllMocks();
- });
- // 每个测试后的清理
- afterEach(() => {
- vi.clearAllMocks();
- });
- // 全局测试工具函数
- globalThis.createTestContext = () => ({
- timestamp: new Date().toISOString(),
- requestId: `test_${Math.random().toString(36).substr(2, 9)}`
- });
- // 类型声明
- declare global {
- // eslint-disable-next-line no-var
- var createTestContext: () => {
- timestamp: string;
- requestId: string;
- };
- }
- export {};
|