Files
esengine/tests/setup.ts

72 lines
1.6 KiB
TypeScript
Raw Normal View History

/**
* Jest
*
*
*/
// 设置测试超时时间(毫秒)
jest.setTimeout(10000);
// 模拟控制台方法以减少测试输出噪音
const originalConsoleLog = console.log;
const originalConsoleWarn = console.warn;
const originalConsoleError = console.error;
// 在测试环境中可以选择性地静默某些日志
beforeAll(() => {
// 可以在这里设置全局的模拟或配置
});
afterAll(() => {
// 清理全局资源
});
// 每个测试前的清理
beforeEach(() => {
// 清理定时器
jest.clearAllTimers();
});
afterEach(() => {
// 恢复所有模拟
jest.restoreAllMocks();
});
// 导出测试工具函数
export const TestUtils = {
/**
*
* @param ms
*/
delay: (ms: number): Promise<void> => {
return new Promise(resolve => setTimeout(resolve, ms));
},
/**
*
* @param condition
* @param timeout
* @param interval
*/
waitFor: async (
condition: () => boolean,
timeout: number = 5000,
interval: number = 10
): Promise<void> => {
const start = Date.now();
while (!condition() && Date.now() - start < timeout) {
await TestUtils.delay(interval);
}
if (!condition()) {
throw new Error(`等待条件超时 (${timeout}ms)`);
}
},
/**
*
* @param ms
*/
advanceTime: (ms: number): void => {
jest.advanceTimersByTime(ms);
}
};