I am trying to test the rejection of one of my axios request call using moxios and jest. The method invoke (calling axios.get) throws error but catch block is not invoked.
My test code snippet is shown below,
it('should call errorHandler method when request is rejected', done => {
const errorResp = {
status: 400,
response: {message: 'invalid data'}
};
let mockFunction = jest.fn();
Axios.getRaw('/test').then(mockFunction);
moxios.wait(async () => {
let req = moxios.requests.mostRecent();
try {
let rejection = await req.reject(errorResp);
console.log('rejection', rejection);// rejection is undefined
done();
} catch (e) {
console.log(e);
// test assertions
done(e);
}
})
});
getRaw method,
const API_WRAPPER = TryCatchHandler.genericTryCatch;
export default {
getRaw: path => API_WRAPPER(axios.get(`${SERVER_DOMAIN}${path}`)),
}
API_WRAPPER method,
export default {
genericTryCatch: async (executionMethod) => {
try {
const response = await executionMethod;
return response;
} catch (error) {
return ApiError.errorHandler(error);
}
}
}
errorHandler method,
export default {
errorHandler: error => {
const {status} = error;
switch (status) {
case 400:
case 401:
case 403:
case 404:
case 409:
case 417:
case 500:
case 502:
console.log("Error Handler says:", error);
return error.response.data;
default:
console.log("Error Handler says:", error);
let errorObj = {
errorMsg: error.message,
stack: error.stack
};
return errorObj;
}
}
}
As the catch block is not invoked I am not able to assert my test cases. How do I get the catch invoked?