Network error with Axios in Jest tests with MSW 2.x #1915
-
Hi, I try to migrate a project from 1.x to 2.x but I have tests failing with 2.x because of "Network Error". The Axios call that causes the problem : axios.post(
"/oauth/web-module",
new URLSearchParams({ accessCode: username, password: password, grant_type: "password" })
); The way I mock it with MSW : http.post("/oauth/web-module", async () => {
return new HttpResponse(null, { status: 200 });
}), I have no network error when I call Axios will axios.post(
"/oauth/web-module",
null
); So it looks like from 2.x MSW can't map this request with the mocks. I thought the mapping was made from url and HTTP verb. Something changed on this with v2.x ? |
Beta Was this translation helpful? Give feedback.
Replies: 16 comments 15 replies
-
I believe you can fix it using this syntax :
It will automatically cause a Network Error. |
Beta Was this translation helpful? Give feedback.
-
Hi, @chawax. No, nothing has changed in regards to request mapping. Can you please submit a reproduction repository for this? I then would be able to help you solve this issue. I'm rather certain it's a configuration issue on your end (missing base URL for Axios, for example). I don't rule out the possibility of an issue in MSW either. |
Beta Was this translation helpful? Give feedback.
-
Hi all, I'm experiencing the same issuee. Trying to migrate to msw 2 and all the axios calls in my jest+testing-library tests, that would fetch data, are erroring. There might be something around the response body mapping. I needed to extend the polyfill found in the migration docs, so I don't get a ReadableStream error. const { Blob, File } = require('node:buffer')
const { TextDecoder, TextEncoder, ReadableStream } = require('node:util')
Object.defineProperties(globalThis, {
TextDecoder: { value: TextDecoder },
TextEncoder: { value: TextEncoder },
ReadableStream: { value: ReadableStream },
})
const { fetch, Headers, FormData, Request, Response } = require('undici')
Object.defineProperties(globalThis, {
fetch: { value: fetch, writable: true },
Blob: { value: Blob },
File: { value: File },
Headers: { value: Headers },
FormData: { value: FormData },
Request: { value: Request },
Response: { value: Response },
}) added these 2 bits to my jest config: testEnvironmentOptions: {
customExportConditions: [''],
},
setupFiles: ['./jest.polyfills.js'], example handler function: const api_v1_materials = ({ responseBodyOverride } = {}) => {
return http.get('/api/v1/materials', () => {
return HttpResponse.json(responseBodyOverride ?? documents, {
headers: {
'Content-Type': 'application/json',
},
})
})
} I tried with and without setting the Content-Type header but it makes no difference.
Would love to put together an example code, but I'm not sure I'll have the time it's a work project. Thanks for looking into it :) |
Beta Was this translation helpful? Give feedback.
-
Hello! |
Beta Was this translation helpful? Give feedback.
-
I'm experiencing the same issue too. |
Beta Was this translation helpful? Give feedback.
-
I have the same issue too. |
Beta Was this translation helpful? Give feedback.
-
Same issue (Network error) for post calls . |
Beta Was this translation helpful? Give feedback.
-
Same issue |
Beta Was this translation helpful? Give feedback.
-
same issue, i think this should be in the issues section msw: v2.0.11 |
Beta Was this translation helpful? Give feedback.
-
I had the same error with axios msw2 and jest and it was solved by using the jest config of @agnesmeri-olio but downgrading undici to v5 from v6 |
Beta Was this translation helpful? Give feedback.
-
Is there a way to use something else apart from |
Beta Was this translation helpful? Give feedback.
-
I was experiencing the same issue - making sure the correct Node version is installed and downgrading undici, along with the polyfill / jest config updates mentioned above worked for me! |
Beta Was this translation helpful? Give feedback.
-
SolutionSee this for the recommended solution. |
Beta Was this translation helpful? Give feedback.
-
Hey Folks, for understanding the purpose and to be confident in adopting this solution. Can someone explain why [email protected] is working for some people and, for others, [email protected] is working fine? |
Beta Was this translation helpful? Give feedback.
-
meet same issue when use msw2 with axios and jest |
Beta Was this translation helpful? Give feedback.
-
Just in case someone is running into the same problem - although I'm not using JEST, but vitest... The following code throws an error, BECAUSE the axios call is made in directly in "describe". import { expect, it, describe } from "vitest";
import axios from "axios";
describe("testing axios network error", async () => {
//it("makes the error go away", async () => {
await axios.get("http://localhost:8000/components/de/urls");
expect(true).toBe(true);
//});
}); |
Beta Was this translation helpful? Give feedback.
I had the same error with axios msw2 and jest and it was solved by using the jest config of @agnesmeri-olio but downgrading undici to v5 from v6
"undici": "^5.0.0",