-
Notifications
You must be signed in to change notification settings - Fork 1
/
mockFetch.ts
31 lines (30 loc) · 964 Bytes
/
mockFetch.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
import type { Response } from "node-fetch";
import { Headers } from "node-fetch";
export const mockFetch =
(
{ url = "/", body = {}, status = 200, headers = {} } = {
url: "/",
body: {},
status: 200,
headers: {},
}
): Promise<
Omit<Response, "type" | "textConverted" | "body" | "bodyUsed" | "clone">
> => {
return Promise.resolve({
ok: status <= 399,
statusText: "OK",
headers: new Headers(headers),
status,
arrayBuffer: () => Promise.resolve(Buffer.from(JSON.stringify(body))),
buffer: () => Promise.resolve(Buffer.from(JSON.stringify(body))),
blob: () => Promise.resolve(new Blob([JSON.stringify(body)])),
json: () => Promise.resolve(body),
text: () => Promise.resolve(JSON.stringify(body)),
clone: () => mockFetch({ url, body, status, headers }),
redirected: false,
size: JSON.stringify(body).length,
url,
timeout: 0,
});
};