/** * 全局测试配置 * 后端测试无需 DOM 环境,前端测试依赖 jsdom 及 antd polyfill */ // 仅当前端测试需要时初始化 jsdom(所有测试共享 preload,后端测试也在此环境中运行) import { JSDOM } from "jsdom"; const dom = new JSDOM("
", { pretendToBeVisual: true, url: "http://localhost", }); globalThis.document = dom.window.document; globalThis.window = dom.window as unknown as typeof globalThis & Window; globalThis.navigator = dom.window.navigator; globalThis.HTMLElement = dom.window.HTMLElement; globalThis.HTMLBodyElement = dom.window.HTMLBodyElement; globalThis.HTMLHtmlElement = dom.window.HTMLHtmlElement; globalThis.Element = dom.window.Element; globalThis.getComputedStyle = (element: Element, pseudoElt?: null | string) => { return dom.window.getComputedStyle(element, pseudoElt ? undefined : pseudoElt); }; if (!globalThis.document.body) { const body = globalThis.document.createElement("body"); globalThis.document.documentElement.appendChild(body); } const nodeProto = dom.window.Node.prototype; const elementProto = dom.window.Element.prototype; const htmlElementProto = dom.window.HTMLElement.prototype; const attachEventFn = () => {}; const detachEventFn = () => {}; Object.defineProperty(nodeProto, "attachEvent", { configurable: true, value: attachEventFn, writable: true }); Object.defineProperty(nodeProto, "detachEvent", { configurable: true, value: detachEventFn, writable: true }); Object.defineProperty(elementProto, "attachEvent", { configurable: true, value: attachEventFn, writable: true }); Object.defineProperty(elementProto, "detachEvent", { configurable: true, value: detachEventFn, writable: true }); Object.defineProperty(htmlElementProto, "attachEvent", { configurable: true, value: attachEventFn, writable: true }); Object.defineProperty(htmlElementProto, "detachEvent", { configurable: true, value: detachEventFn, writable: true }); const originalStderrWrite = process.stderr.write.bind(process.stderr); process.stderr.write = (chunk: string | Uint8Array, encodingOrCb?: unknown, cb?: unknown) => { const str = typeof chunk === "string" ? chunk : Buffer.from(chunk).toString(); if (str.includes("NaN") && str.includes("height") && str.includes("css style property")) return true; return originalStderrWrite( chunk, encodingOrCb as Parameters