How do you mock an error body correctly to work with Axios? #1872
-
I'm setting up a msw server with const server = setupServer(
http.post('http://172.17.0.1:3001/auth/confirm', ({ request }) => {
const newPost = await request.json() as Request;
allPosts.push(newPost);
return HttpResponse.json(
{ body: { errorCode: 123 } },
{ status: 400 },
);
})
); And then logging the response with try {
const response = await axios( ...config )
} catch (error) {
console.log(error)
} which logs the following data: '',
status: 400,
statusText: 'Bad Request',
headers: Object [AxiosHeaders] { 'content-type': 'application/json' }, Have I set up the server incorrectly? How should I properly configure the server to return a body in the response? |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
Hey, @MyGuyCai. Your setup seems to be correct. To confirm that, I've added a test case for mocking error responses with Axios (#1891), which passes without issues. I encourage you to take a look at that test and see if you can spot any difference with what you have. I suspect you get a false-positive 400 response, to which the empty If your exploration proves unfruitful, please submit a reproduction repository where I can reproduce this bug. I'd be happy to take a look and help you figure it out. |
Beta Was this translation helpful? Give feedback.
Hey, @MyGuyCai. Your setup seems to be correct. To confirm that, I've added a test case for mocking error responses with Axios (#1891), which passes without issues. I encourage you to take a look at that test and see if you can spot any difference with what you have. I suspect you get a false-positive 400 response, to which the empty
data
points at.If your exploration proves unfruitful, please submit a reproduction repository where I can reproduce this bug. I'd be happy to take a look and help you figure it out.