From 92e7c4bbc99ad1cbf392a2ccd5ea4b1dfe311243 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mateusz=20Burzy=C5=84ski?= Date: Wed, 3 Jul 2024 19:01:30 +0200 Subject: [PATCH] Add missing fetch imports (#593) * Add missing fetch imports * fixed undici mocking * actually fix it --- packages/replay/src/auth.ts | 1 + packages/shared/src/cachedFetch.test.ts | 21 +++++++++++++-------- packages/shared/src/cachedFetch.ts | 2 ++ 3 files changed, 16 insertions(+), 8 deletions(-) diff --git a/packages/replay/src/auth.ts b/packages/replay/src/auth.ts index cbdaf81b..652b72c1 100644 --- a/packages/replay/src/auth.ts +++ b/packages/replay/src/auth.ts @@ -1,3 +1,4 @@ +import fetch from "node-fetch"; import { spawn } from "child_process"; import { createHash } from "crypto"; import dbg from "./debug"; diff --git a/packages/shared/src/cachedFetch.test.ts b/packages/shared/src/cachedFetch.test.ts index 4a15a979..8768f69d 100644 --- a/packages/shared/src/cachedFetch.test.ts +++ b/packages/shared/src/cachedFetch.test.ts @@ -1,5 +1,8 @@ import assert from "assert"; -import { cachedFetch, resetCache } from "./cachedFetch"; +import type { + cachedFetch as cachedFetchStatic, + resetCache as resetCacheStatic, +} from "./cachedFetch"; class Response { public status: number; @@ -21,20 +24,22 @@ const failedResponse = new Response(500, "error"); const successResponse = new Response(200, "ok"); describe("cachedFetch", () => { - let globalFetch: typeof fetch; + let cachedFetch: typeof cachedFetchStatic; + let resetCache: typeof resetCacheStatic; let mockFetch: jest.Mock; beforeEach(() => { - globalFetch = global.fetch; - mockFetch = global.fetch = jest.fn(async (url: string) => { - return successResponse; - }) as jest.Mock; + jest.mock("undici"); + + mockFetch = require("undici").fetch; + mockFetch.mockReturnValue(successResponse); + + ({ cachedFetch, resetCache } = require("./cachedFetch")); }); afterEach(() => { - global.fetch = globalFetch; - resetCache(); + mockFetch.mockClear(); }); it("should return a successful response", async () => { diff --git a/packages/shared/src/cachedFetch.ts b/packages/shared/src/cachedFetch.ts index 59a1981b..c99f4f3c 100644 --- a/packages/shared/src/cachedFetch.ts +++ b/packages/shared/src/cachedFetch.ts @@ -1,3 +1,5 @@ +import { fetch } from "undici"; + type CacheEntry = { json: any | null; status: number; statusText: string }; export const cache: Map = new Map();