-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathjest-fetch-mock.spec.ts
29 lines (25 loc) · 1.08 KB
/
jest-fetch-mock.spec.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
"use strict";
import { describe, test, expect } from "@jest/globals";
import _fetchMock from "jest-fetch-mock";
// import createFetchCache from "fetch-mock-cache"
// import MemoryStore from "fetch-mock-cache/stores/memory";
import createFetchCache from "../../../../src/runtimes/node.js";
import MemoryStore from "../../../../src/stores/memory.js";
const fetchMock = _fetchMock.default;
fetchMock.enableMocks();
describe("jest-fetch-mock", () => {
const fetchCache = createFetchCache({ Store: MemoryStore });
const url = "http://echo.jsontest.com/key/value/one/two";
const expectedResponse = { one: "two", key: "value" };
test("memoryStore", async () => {
for (let i = 0; i < 2; i++) {
// If you get a TypeError here, make sure @types/jest is instaled.
fetchMock.mockImplementationOnce(fetchCache);
const response = await fetch(url);
const data = await response.json();
const expectedCacheHeader = i === 0 ? "MISS" : "HIT";
expect(response.headers.get("X-FMC-Cache")).toBe(expectedCacheHeader);
expect(data).toEqual(expectedResponse);
}
});
});