fetch-mock
fetch-mock copied to clipboard
Response.error() is not being treated as a response
trafficstars
Here's a simple test that would demonstrate the problem.
import fetchMock from 'fetch-mock-jest';
it("should work with Response.error()", async () => {
fetchMock.mock("https://trajano.net/bad-request", Response.error());
const response = await fetch("https://trajano.net/bad-request");
expect(response.status).toBe(0);
});
Workaround
let fetchConfigResponse: (new () => Response) | undefined;
beforeEach(() => {
fetchConfigResponse = fetchMock.config.Response;
})
afterEach(() => {
fetchMock.mockReset();
fetchMock.config.Response = fetchConfigResponse;
})
it("Response.error type check", async () => {
fetchMock.config.Response = Response;
expect(Response.error() instanceof Response).toBe(true);
expect(fetchMock.config.Response?.prototype.isPrototypeOf(Response.error())).toBe(true);
});