fix: keep browser fetch helper under test support · openclaw/openclaw@ca972f6
steipete
·
2026-04-29
·
via Recent Commits to openclaw:main
File tree
extensions/browser/src/browser
| Original file line number | Diff line number | Diff line change |
|---|
|
1 | | -export { getBrowserTestFetch, type BrowserTestFetch } from "../test-support-fetch.js"; |
| 1 | +import { createRequire } from "node:module"; |
| 2 | + |
| 3 | +type FetchLike = ((input: string | URL, init?: RequestInit) => Promise<Response>) & { |
| 4 | +mock?: unknown; |
| 5 | +}; |
| 6 | + |
| 7 | +export type BrowserTestFetch = (input: string | URL, init?: RequestInit) => Promise<Response>; |
| 8 | + |
| 9 | +function isUsableFetch(value: unknown): value is FetchLike { |
| 10 | +return typeof value === "function" && !("mock" in (value as FetchLike)); |
| 11 | +} |
| 12 | + |
| 13 | +export function getBrowserTestFetch(): BrowserTestFetch { |
| 14 | +const require = createRequire(import.meta.url); |
| 15 | +const vitest = (globalThis as { vi?: { doUnmock?: (id: string) => void } }).vi; |
| 16 | +vitest?.doUnmock?.("undici"); |
| 17 | +try { |
| 18 | +delete require.cache[require.resolve("undici")]; |
| 19 | +} catch { |
| 20 | +// Best-effort cache bust for shared-thread test workers. |
| 21 | +} |
| 22 | +const { fetch } = require("undici") as typeof import("undici"); |
| 23 | +if (isUsableFetch(fetch)) { |
| 24 | +return (input, init) => fetch(input, init); |
| 25 | +} |
| 26 | +if (isUsableFetch(globalThis.fetch)) { |
| 27 | +return (input, init) => globalThis.fetch(input, init); |
| 28 | +} |
| 29 | +throw new TypeError("fetch is not a function"); |
| 30 | +} |
此内容由惯性聚合(RSS阅读器)自动聚合整理,仅供阅读参考。 原文来自 — 版权归原作者所有。