refactor: waitFor→findBy 替换 + renderWithBasicProviders + jsdom 条件化回退
This commit is contained in:
@@ -1,10 +1,35 @@
|
||||
/**
|
||||
* 全局测试配置
|
||||
* 后端测试无需 DOM 环境,前端测试依赖 jsdom 及 antd polyfill
|
||||
* 为所有测试初始化 jsdom — bun worker 的 process.argv 无法保证携带文件路径
|
||||
* 噪声过滤对所有测试生效
|
||||
*/
|
||||
|
||||
// 仅当前端测试需要时初始化 jsdom(所有测试共享 preload,后端测试也在此环境中运行)
|
||||
import { JSDOM } from "jsdom";
|
||||
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;
|
||||
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 { JSDOM } = await import("jsdom");
|
||||
|
||||
const dom = new JSDOM("<!DOCTYPE html><html><body></body></html>", {
|
||||
pretendToBeVisual: true,
|
||||
@@ -41,31 +66,6 @@ Object.defineProperty(elementProto, "detachEvent", { configurable: true, value:
|
||||
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<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;
|
||||
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);
|
||||
};
|
||||
|
||||
globalThis.ResizeObserver = class {
|
||||
disconnect() {}
|
||||
observe() {}
|
||||
@@ -128,7 +128,7 @@ globalThis.Selection = class Selection {
|
||||
} as unknown as typeof Selection;
|
||||
globalThis.Range = class Range extends dom.window.DocumentFragment {} as unknown as typeof Range;
|
||||
|
||||
import { afterEach } from "bun:test";
|
||||
const { afterEach } = await import("bun:test");
|
||||
|
||||
afterEach(() => {
|
||||
document.body.innerHTML = "";
|
||||
|
||||
Reference in New Issue
Block a user