When mocking a 404 error the result is
Uncaught UnhandledPromiseRejection: This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). The promise rejected with the reason "Error: error".
Setup a basic Mock
const mockAccountEnquiry = () => {
axios.post.mockImplementation(async (url, testParams) => {
if (url === 'https://blah/v1/payIDEnquiry') {
if (testParams.payID === 'not-found') {
jest.fn().mockReturnValue(Promise.reject(Error('error')));
}
return {
status: 200,
data: {
AccountStatus: {
nppReachable: testParams.nppReachable,
},
},
};
}
});
};
Create a test case
it('Failed request', async() => {
mockAccountEnquiry();
const response = await backend.handler(testData);
expect(axios.post.mock.calls[0][0]).toBe(enquiryUrl);
});
How to mock the 404 request?