Working on a legacy code, I’ve been given the following test (summarised version):
// myservice.test.ts
import fetchMock from "jest-fetch-mock";
// ... other imports from own code omitted for brevity
beforeEach(() => {
fetch.mockClear();
});
it("should return and error when there is any exception", async () => {
let error : Error = new Error("Something went wrong");
fetch.mockReject(error);
let someService : MyService = new MyService();
await someService.someFunction().catch(e => {
expect(e).toEqual(error);
});
expect(fetch).toBeCalledTimes(1);
});
The test runs and passes from the command line and when it’s run from IntelliJ IDEA (Ultimate).
The problem I’d like to solve is that IntelliJ IDEA is not able to recognise Jest Fetch Mock’s mockReject
as a function.
Therefore, I suspect the test is passing because fetch
is correctly the mock version, but IntelliJ IDEA doesn’t realize it; there must be some import
or config missing or something like that.
See the screenshot of what IntelliJ IDEA shows (some code redacted for privacy):
The link to MDN Reference points to https://developer.mozilla.org/en-US/docs/Web/API/fetch
3