Files
knowai/test/setup.ts
tobegold574 b74c01340c feat(image): 新建knowai-ui:1.0.0镜像并完成推送
- 最终完成16个组件的搭建,覆盖常用组件和布局
- 通过tsc eslint完成测试,vitest测试因未知问题放弃(但保留测试文件)

BREAKING CHANGE: 新镜像分支
2025-11-14 14:48:18 +08:00

87 lines
2.2 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/**
* KnowAI UI组件库测试设置文件
* 配置全局测试环境和共享设置
*/
import { vi } from 'vitest';
import { config } from '@vue/test-utils';
// 导入Element Plus的ElIcon组件用于Icon组件测试
import { ElIcon } from 'element-plus';
// 配置Vue测试工具
config.global.components = {
ElIcon // 全局注册ElIcon组件
};
config.global.stubs = {
// 全局组件存根配置
};
// 模拟浏览器API
Object.defineProperty(window, 'localStorage', {
value: {
getItem: vi.fn(),
setItem: vi.fn(),
removeItem: vi.fn(),
clear: vi.fn()
},
writable: true
});
Object.defineProperty(window, 'sessionStorage', {
value: {
getItem: vi.fn(),
setItem: vi.fn(),
removeItem: vi.fn(),
clear: vi.fn()
},
writable: true
});
// 模拟IntersectionObserver
globalThis.IntersectionObserver = vi.fn().mockImplementation(() => ({
observe: vi.fn(),
unobserve: vi.fn(),
disconnect: vi.fn()
}));
// 模拟ResizeObserver
globalThis.ResizeObserver = vi.fn().mockImplementation(() => ({
observe: vi.fn(),
unobserve: vi.fn(),
disconnect: vi.fn()
}));
// 模拟requestAnimationFrame
globalThis.requestAnimationFrame = vi.fn((cb: FrameRequestCallback) => setTimeout(cb, 0));
// 模拟cancelAnimationFrame
globalThis.cancelAnimationFrame = vi.fn((id: number) => clearTimeout(id));
// 为fetch提供一个基本的模拟实现避免birpc超时
globalThis.fetch = vi.fn().mockResolvedValue({
ok: true,
status: 200,
json: vi.fn().mockResolvedValue({}),
text: vi.fn().mockResolvedValue('')
});
// 模拟document相关API
Object.defineProperty(document, 'visibilityState', {
value: 'visible',
writable: true
});
document.onvisibilitychange = vi.fn();
// 设置环境变量
(globalThis as any).process = (globalThis as any).process || {};
(globalThis as any).process.env = (globalThis as any).process.env || {};
(globalThis as any).process.env.NODE_ENV = 'test';
// 禁用控制台警告和错误,避免测试输出过于嘈杂
vi.spyOn(console, 'warn').mockImplementation(() => {});
vi.spyOn(console, 'error').mockImplementation(() => {});
// 注释掉定时器控制避免影响Vitest内部异步操作
// vi.useFakeTimers();