fetch-mock icon indicating copy to clipboard operation
fetch-mock copied to clipboard

Response.error() is not being treated as a response

Open trajano opened this issue 2 years ago • 1 comments
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);
});

trajano avatar Dec 19 '22 06:12 trajano

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);
  });

trajano avatar Dec 19 '22 07:12 trajano