I'm trying to test the catch block of this try/catch.
try {
const response = await request.get({
// ... required params here
});
return response.data;
} catch (error) {
myFunction({
statusCode: error.response.status,
});
throw error;
}
My aim is to test the call of myFunction inside the catch. I want to confirm that the function was called with the correct parameters of { statusCode: 400 } for example.
I also want to test that an error was thrown.
I've written a mockImplementation of request.get in an attempt to get this error response, but it does not seem to call into my catch block.
it("should throw an error and call myFunction with the status code", async () => {
await request.get.mockImplementation(() => {
throw new Error();
// how do I add return of statusCode here?
});
expect(addToNewRelic).toHaveBeenCalledWith(
expect.objectContaining({
productAPIRequestStatusCode: 404,
})
);
});