Skip to content

Commit

Permalink
Add missing fetch imports (#593)
Browse files Browse the repository at this point in the history
* Add missing fetch imports

* fixed undici mocking

* actually fix it
  • Loading branch information
Andarist authored Jul 3, 2024
1 parent fe9b225 commit 92e7c4b
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 8 deletions.
1 change: 1 addition & 0 deletions packages/replay/src/auth.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import fetch from "node-fetch";
import { spawn } from "child_process";
import { createHash } from "crypto";
import dbg from "./debug";
Expand Down
21 changes: 13 additions & 8 deletions packages/shared/src/cachedFetch.test.ts
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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 () => {
Expand Down
2 changes: 2 additions & 0 deletions packages/shared/src/cachedFetch.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { fetch } from "undici";

type CacheEntry = { json: any | null; status: number; statusText: string };

export const cache: Map<string, CacheEntry> = new Map();
Expand Down

0 comments on commit 92e7c4b

Please sign in to comment.