From 6f2c832dee41cf6b5d77a0568abd31152685c7d1 Mon Sep 17 00:00:00 2001 From: George Fu Date: Mon, 28 Oct 2024 18:46:22 +0000 Subject: [PATCH] fix fetch browser tests --- .../src/fetch-http-handler.browser.spec.ts | 25 ++++++++++++++++--- 1 file changed, 22 insertions(+), 3 deletions(-) diff --git a/packages/fetch-http-handler/src/fetch-http-handler.browser.spec.ts b/packages/fetch-http-handler/src/fetch-http-handler.browser.spec.ts index c3ff99adf7c..779a140ed72 100644 --- a/packages/fetch-http-handler/src/fetch-http-handler.browser.spec.ts +++ b/packages/fetch-http-handler/src/fetch-http-handler.browser.spec.ts @@ -6,12 +6,31 @@ import { createRequest } from "./create-request"; import { FetchHttpHandler, keepAliveSupport } from "./fetch-http-handler"; vi.mock("./create-request", async () => { - const actual: any = await vi.importActual("./create-request"); return { - createRequest: vi.fn().mockImplementation(actual.createRequest), + createRequest: vi.fn().mockImplementation((_url, options) => { + const url = new URL(_url); + return { + protocol: url.protocol, + hostname: url.hostname, + ...options, + } as any; + }), }; }); +vi.spyOn(global, "fetch").mockImplementation((async () => { + return { + headers: { + entries() { + return []; + }, + }, + async blob() { + return undefined; + }, + }; +}) as any); + (typeof Blob === "function" ? describe : describe.skip)(FetchHttpHandler.name, () => { interface MockHttpRequestOptions { method?: string; @@ -23,7 +42,7 @@ vi.mock("./create-request", async () => { } const getMockHttpRequest = (options: MockHttpRequestOptions): HttpRequest => - new HttpRequest({ hostname: "example.com", ...options }); + new HttpRequest({ hostname: "localhost", protocol: "http", ...options }); describe("fetch", () => { beforeAll(() => {