From b7ca9082efd038cafa8fdc3004411fd642d84da7 Mon Sep 17 00:00:00 2001 From: Peter Steinberger Date: Fri, 13 Mar 2026 23:57:16 +0000 Subject: [PATCH] test: tighten fetch helper coverage --- src/infra/fetch.test.ts | 68 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 68 insertions(+) diff --git a/src/infra/fetch.test.ts b/src/infra/fetch.test.ts index fb01f27de12..1ee31b6182b 100644 --- a/src/infra/fetch.test.ts +++ b/src/infra/fetch.test.ts @@ -60,6 +60,42 @@ describe("wrapFetchWithAbortSignal", () => { expect((seenInit as (RequestInit & { duplex?: string }) | undefined)?.duplex).toBe("half"); }); + it("adds duplex when the input Request already carries the body", async () => { + let seenInit: RequestInit | undefined; + const fetchImpl = withFetchPreconnect( + vi.fn(async (_input: RequestInfo | URL, init?: RequestInit) => { + seenInit = init; + return {} as Response; + }), + ); + + const wrapped = wrapFetchWithAbortSignal(fetchImpl); + + await wrapped(new Request("https://example.com", { method: "POST", body: "hi" })); + + expect((seenInit as (RequestInit & { duplex?: string }) | undefined)?.duplex).toBe("half"); + }); + + it("preserves an existing duplex init field", async () => { + let seenInit: RequestInit | undefined; + const fetchImpl = withFetchPreconnect( + vi.fn(async (_input: RequestInfo | URL, init?: RequestInit) => { + seenInit = init; + return {} as Response; + }), + ); + + const wrapped = wrapFetchWithAbortSignal(fetchImpl); + + await wrapped("https://example.com", { + method: "POST", + body: "hi", + duplex: "half", + } as RequestInit & { duplex: "half" }); + + expect(seenInit).toMatchObject({ duplex: "half" }); + }); + it("converts foreign abort signals to native controllers", async () => { let seenSignal: AbortSignal | undefined; const fetchImpl = withFetchPreconnect( @@ -167,6 +203,26 @@ describe("wrapFetchWithAbortSignal", () => { expect(removeEventListener).not.toHaveBeenCalled(); }); + it("passes through foreign signal-like objects without addEventListener", async () => { + let seenSignal: AbortSignal | undefined; + const fetchImpl = withFetchPreconnect( + vi.fn(async (_input: RequestInfo | URL, init?: RequestInit) => { + seenSignal = init?.signal as AbortSignal | undefined; + return {} as Response; + }), + ); + const wrapped = wrapFetchWithAbortSignal(fetchImpl); + + const fakeSignal = { + aborted: false, + removeEventListener: vi.fn(), + } as unknown as AbortSignal; + + await wrapped("https://example.com", { signal: fakeSignal }); + + expect(seenSignal).toBe(fakeSignal); + }); + it("returns the same function when called with an already wrapped fetch", () => { const fetchImpl = withFetchPreconnect(vi.fn(async () => ({ ok: true }) as Response)); const wrapped = wrapFetchWithAbortSignal(fetchImpl); @@ -194,3 +250,15 @@ describe("wrapFetchWithAbortSignal", () => { expect(seenThis).toBe(fetchImpl); }); }); + +describe("resolveFetch", () => { + it("returns undefined when neither an explicit nor global fetch exists", () => { + const previousFetch = globalThis.fetch; + vi.stubGlobal("fetch", undefined); + try { + expect(resolveFetch(undefined)).toBeUndefined(); + } finally { + vi.stubGlobal("fetch", previousFetch); + } + }); +});