How to handle Jest/Axios error without using try/catch?

  Kiến thức lập trình

is there existing another way how to handle error message while using JS/TS with Axios and Jest libs in order to get more information why request get failed ?

For example:

const req = await callApi(payload) 
expect(req.status).toEqual(200)

Let’s assume that check above get failed on status 400
With code above you received only

Request failed with status code 400 and has no info which validation failed.

I know about try/catch – but something telling me that using try/catch on automation test is not good idea. Maybe i am wrong. Moreover, if you are using try/catch you have to expect status 200 too in catch block, because if you do not use expect there, the test will be facing like success, despite of fact that error occurred.

Another way is using promise – then – construction. Actually its worse that try/catch 🙂 you know, a lot of unnecessary and complex code.

So, is it possible somehow extend error handler / function in Axios or Jest in order to receive info?

LEAVE A COMMENT