November 2019
Beginner
804 pages
20h 1m
English
Now, without implying that it is actually a good idea, let's see how we can spy on method calls using Jest. We'll verify that the checkResponseStatus method gets called as expected:
it('should call checkResponseStatus', async () => {
fetchMock.mockResponse(
JSON.stringify(dummyValidData),
{status: 200}
);
const checkResponseStatusSpy = jest.spyOn(sut, "checkResponseStatus");
await sut.getAllCountries().catch(() => {
fail("Should not fail");
});
expect(checkResponseStatusSpy).toHaveBeenCalledTimes(1);
});
Using the jest.spyOn function, we tell Jest that we want to spy on the checkResponseStatus method. Then, after having called our getAllCountries method, we can verify whether the object that we're spying on has ...
Read now
Unlock full access