chore: jsdom 迁移至 happy-dom
This commit is contained in:
18
tests/happydom.ts
Normal file
18
tests/happydom.ts
Normal file
@@ -0,0 +1,18 @@
|
||||
import { GlobalRegistrator } from "@happy-dom/global-registrator";
|
||||
|
||||
// 保存 Node/Bun 原生实现,避免被 happy-dom 覆盖导致服务端测试失败
|
||||
const nativeFetch = globalThis.fetch;
|
||||
const nativeResponse = globalThis.Response;
|
||||
const nativeRequest = globalThis.Request;
|
||||
const nativeHeaders = globalThis.Headers;
|
||||
const nativeFormData = globalThis.FormData;
|
||||
const nativeBlob = globalThis.Blob;
|
||||
|
||||
GlobalRegistrator.register();
|
||||
|
||||
globalThis.fetch = nativeFetch;
|
||||
globalThis.Response = nativeResponse;
|
||||
globalThis.Request = nativeRequest;
|
||||
globalThis.Headers = nativeHeaders;
|
||||
globalThis.FormData = nativeFormData;
|
||||
globalThis.Blob = nativeBlob;
|
||||
@@ -1,6 +1,6 @@
|
||||
/**
|
||||
* 全局测试配置
|
||||
* 为所有测试初始化 jsdom — bun worker 的 process.argv 无法保证携带文件路径
|
||||
* happy-dom 已在 happydom.ts 中通过 GlobalRegistrator.register() 注入全局对象
|
||||
* 噪声过滤对所有测试生效
|
||||
*/
|
||||
|
||||
@@ -29,32 +29,9 @@ console.warn = (...args: unknown[]) => {
|
||||
originalConsoleWarn(...args);
|
||||
};
|
||||
|
||||
const { JSDOM } = await import("jsdom");
|
||||
|
||||
const dom = new JSDOM("<!DOCTYPE html><html><body></body></html>", {
|
||||
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 nodeProto = Node.prototype;
|
||||
const elementProto = Element.prototype;
|
||||
const htmlElementProto = HTMLElement.prototype;
|
||||
|
||||
const attachEventFn = () => {};
|
||||
const detachEventFn = () => {};
|
||||
@@ -93,40 +70,13 @@ globalThis.IntersectionObserver = class {
|
||||
globalThis.requestAnimationFrame = (cb: FrameRequestCallback) => setTimeout(cb, 16);
|
||||
globalThis.cancelAnimationFrame = (id: number) => clearTimeout(id);
|
||||
|
||||
Object.defineProperty(dom.window, "matchMedia", {
|
||||
value: (query: string) => ({
|
||||
addEventListener: () => {},
|
||||
addListener: () => {},
|
||||
dispatchEvent: () => true,
|
||||
matches: false,
|
||||
media: query,
|
||||
onchange: null,
|
||||
removeEventListener: () => {},
|
||||
removeListener: () => {},
|
||||
}),
|
||||
writable: true,
|
||||
});
|
||||
Element.prototype.scrollTo = () => {};
|
||||
Element.prototype.scrollIntoView = () => {};
|
||||
|
||||
dom.window.Element.prototype.scrollTo = () => {};
|
||||
dom.window.Element.prototype.scrollIntoView = () => {};
|
||||
|
||||
Object.defineProperty(dom.window, "customElements", {
|
||||
value: {
|
||||
define: () => {},
|
||||
get: () => undefined,
|
||||
},
|
||||
writable: true,
|
||||
});
|
||||
|
||||
globalThis.customElements = dom.window.customElements;
|
||||
|
||||
globalThis.ShadowRoot = class ShadowRoot extends dom.window.DocumentFragment {} as unknown as typeof ShadowRoot;
|
||||
globalThis.SVGElement = class SVGElement extends dom.window.Element {} as unknown as typeof SVGElement;
|
||||
globalThis.Selection = class Selection {
|
||||
addRange() {}
|
||||
removeAllRanges() {}
|
||||
} as unknown as typeof Selection;
|
||||
globalThis.Range = class Range extends dom.window.DocumentFragment {} as unknown as typeof Range;
|
||||
|
||||
const { afterEach } = await import("bun:test");
|
||||
|
||||
|
||||
Reference in New Issue
Block a user