96 lines
3.2 KiB
TypeScript
96 lines
3.2 KiB
TypeScript
/**
|
|
* 全局测试配置
|
|
* happy-dom 已在 happydom.ts 中通过 GlobalRegistrator.register() 注入全局对象
|
|
* 噪声过滤对所有测试生效
|
|
*/
|
|
|
|
declare global {
|
|
// eslint-disable-next-line no-var
|
|
var IS_REACT_ACT_ENVIRONMENT: boolean;
|
|
}
|
|
|
|
globalThis.IS_REACT_ACT_ENVIRONMENT = 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<typeof process.stderr.write>[1],
|
|
cb as Parameters<typeof process.stderr.write>[2],
|
|
);
|
|
};
|
|
|
|
const originalConsoleError = console.error;
|
|
console.error = (...args: unknown[]) => {
|
|
const message = args.map(String).join(" ");
|
|
if (message.includes("NaN") && message.includes("height") && message.includes("css style property")) return;
|
|
if (message.includes("not wrapped in act")) return;
|
|
originalConsoleError(...args);
|
|
};
|
|
|
|
const originalConsoleWarn = console.warn;
|
|
console.warn = (...args: unknown[]) => {
|
|
const message = args.map(String).join(" ");
|
|
if (message.includes("NaN") && message.includes("height") && message.includes("css style property")) return;
|
|
originalConsoleWarn(...args);
|
|
};
|
|
|
|
const nodeProto = Node.prototype;
|
|
const elementProto = Element.prototype;
|
|
const htmlElementProto = 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 });
|
|
|
|
globalThis.ResizeObserver = class {
|
|
disconnect() {}
|
|
observe() {}
|
|
unobserve() {}
|
|
};
|
|
|
|
globalThis.MutationObserver = class {
|
|
disconnect() {}
|
|
observe() {}
|
|
takeRecords() {
|
|
return [];
|
|
}
|
|
unobserve() {}
|
|
};
|
|
|
|
globalThis.IntersectionObserver = class {
|
|
disconnect() {}
|
|
observe() {}
|
|
takeRecords() {
|
|
return [];
|
|
}
|
|
unobserve() {}
|
|
} as unknown as typeof IntersectionObserver;
|
|
|
|
globalThis.requestAnimationFrame = (cb: FrameRequestCallback) => setTimeout(cb, 16);
|
|
globalThis.cancelAnimationFrame = (id: number) => clearTimeout(id);
|
|
|
|
Element.prototype.scrollTo = () => {};
|
|
Element.prototype.scrollIntoView = () => {};
|
|
|
|
globalThis.Selection = class Selection {
|
|
addRange() {}
|
|
removeAllRanges() {}
|
|
} as unknown as typeof Selection;
|
|
|
|
const { afterEach } = await import("bun:test");
|
|
const { cleanup } = await import("@testing-library/react");
|
|
|
|
afterEach(() => {
|
|
cleanup();
|
|
document.body.innerHTML = "";
|
|
});
|