macos-web/tests/setup.ts
2026-07-23 15:16:57 +08:00

76 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.

/**
* tests/setup.ts — vitest 全局 setup
*
* 在所有测试之前执行,负责:
* 1. 导入 CSS避免 import 报错)
* 2. polyfill jsdom 缺失的浏览器 API
* 3. 每个测试后全局清理
*/
import { beforeAll, afterEach, vi } from 'vitest'
// 导入全局 CSS
import '../src/styles/base.css'
import '../src/styles/window.css'
import '../src/styles/desktop.css'
import '../src/styles/apps.css'
import '../src/styles/apps2.css'
// ============================================================
// jsdom polyfills
// ============================================================
beforeAll(() => {
// matchMedia
if (!window.matchMedia) {
Object.defineProperty(window, 'matchMedia', {
writable: true,
value: vi.fn().mockImplementation((query: string) => ({
matches: false,
media: query,
onchange: null,
addListener: vi.fn(),
removeListener: vi.fn(),
addEventListener: vi.fn(),
removeEventListener: vi.fn(),
dispatchEvent: vi.fn(() => false),
})),
})
}
// scrollIntoView
if (!Element.prototype.scrollIntoView) {
Element.prototype.scrollIntoView = vi.fn()
}
// requestAnimationFrame / cancelAnimationFrame
if (!window.requestAnimationFrame) {
let rafId = 0
const callbacks = new Map<number, FrameRequestCallback>()
window.requestAnimationFrame = vi.fn((cb: FrameRequestCallback) => {
const handle = ++rafId
callbacks.set(handle, cb)
setTimeout(() => {
const fn = callbacks.get(handle)
if (fn) { callbacks.delete(handle); fn(Date.now()) }
}, 16)
return handle
})
window.cancelAnimationFrame = vi.fn((handle: number) => { callbacks.delete(handle) })
}
// structuredClone 兜底
if (!window.structuredClone) {
;(window as any).structuredClone = (obj: any) => JSON.parse(JSON.stringify(obj))
}
})
// ============================================================
// 每个测试后全局清理
// ============================================================
afterEach(() => {
// 恢复真实 timers防止 fake timers 泄漏)
vi.useRealTimers()
// 清理 localStorage
localStorage.clear()
})