实现 type: ping checker,通过 Bun.spawn 调用系统 ping 命令,自行实现跨平台 输出解析器(Linux/macOS/Windows 含中文 locale),支持 alive、丢包率、延迟、 耗时等 expect 断言,复用现有 checker 架构零外部依赖。 包含完整的类型定义、TypeBox schema、语义校验、命令构建、解析、断言、执行、 注册、配置加载测试,以及 probe-config.schema.json 更新和文档更新。 审查修复:提取 buildPingCommand 为独立纯函数并补充跨平台单测,补充 maxDurationMs/maxAvgLatencyMs 类型非法和空字符串 host 边界测试用例。 变更已归档,delta specs 已同步至 main specs。
115 lines
3.6 KiB
TypeScript
115 lines
3.6 KiB
TypeScript
/**
|
|
* 全局测试配置
|
|
* 主要为后端测试提供基础环境
|
|
* 组件测试使用各自的 test-utils.tsx
|
|
*/
|
|
|
|
/* eslint-disable @typescript-eslint/no-empty-function */
|
|
// Set up jsdom for ALL tests (both backend and frontend)
|
|
import { JSDOM } from "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.Element = dom.window.Element;
|
|
globalThis.getComputedStyle = dom.window.getComputedStyle;
|
|
|
|
// Ensure document.body exists
|
|
if (!globalThis.document.body) {
|
|
const body = globalThis.document.createElement("body");
|
|
globalThis.document.documentElement.appendChild(body);
|
|
}
|
|
|
|
// CRITICAL: Set up polyfills BEFORE any other imports
|
|
// This ensures @testing-library/react sees these when it loads
|
|
|
|
// IE-style event handling polyfill (React fallback)
|
|
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 });
|
|
|
|
// Other polyfills
|
|
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);
|
|
|
|
Object.defineProperty(dom.window, "matchMedia", {
|
|
value: (query: string) => ({
|
|
addEventListener: () => {},
|
|
addListener: () => {},
|
|
dispatchEvent: () => true,
|
|
matches: false,
|
|
media: query,
|
|
onchange: null,
|
|
removeEventListener: () => {},
|
|
removeListener: () => {},
|
|
}),
|
|
writable: true,
|
|
});
|
|
|
|
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;
|
|
|
|
// Mock @number-flow/react globally (custom elements not supported in jsdom)
|
|
import { afterEach, mock } from "bun:test";
|
|
import { createElement } from "react";
|
|
|
|
void mock.module("@number-flow/react", () => {
|
|
const NumberFlow = () => createElement("span", { "data-testid": "number-flow" });
|
|
const NumberFlowGroup = ({ children }: { children: unknown }) => children;
|
|
return { default: NumberFlow, NumberFlowGroup };
|
|
});
|
|
|
|
afterEach(() => {
|
|
document.body.innerHTML = "";
|
|
});
|