import { vi } from 'vitest' // 路由守卫依赖 nprogress,jsdom 下避免其 DOM 副作用 vi.mock('nprogress', () => ({ default: { configure: vi.fn(), start: vi.fn(), done: vi.fn() }, configure: vi.fn(), start: vi.fn(), done: vi.fn(), })) // ----- jsdom 缺失的浏览器 API polyfill ----- // 自建 UI 组件(modal/select/dropdown/tree 等)常依赖以下全局对象,jsdom 未实现,需补桩。 // ResizeObserver:组件监听元素尺寸时使用 if (!globalThis.ResizeObserver) { globalThis.ResizeObserver = class { observe() {} unobserve() {} disconnect() {} } } // IntersectionObserver:虚拟列表/懒加载等使用 if (!globalThis.IntersectionObserver) { globalThis.IntersectionObserver = class { constructor() {} observe() {} unobserve() {} disconnect() {} takeRecords() { return [] } } } // matchMedia:主题/响应式/偏好判断使用 if (!globalThis.matchMedia) { globalThis.matchMedia = (query) => ({ matches: false, media: query, onchange: null, addListener: vi.fn(), removeListener: vi.fn(), addEventListener: vi.fn(), removeEventListener: vi.fn(), dispatchEvent: vi.fn(), }) } // 元素滚动方法:jsdom 未实现会抛 "not implemented" if (!Element.prototype.scrollTo) { Element.prototype.scrollTo = vi.fn() } if (!Element.prototype.scrollIntoView) { Element.prototype.scrollIntoView = vi.fn() }