Replies: 1 comment 5 replies
-
Neither You probably want to polyfill them, or even mock them. You might want to try the package import { Request, Response, fetch } from "cross-fetch";
vi.stubGlobal("Response", Response);
vi.stubGlobal("Request", Request);
vi.stubGlobal("fetch", fetch); // Ideally you'd want to mock this with a function that returns the expected result, for each test. |
Beta Was this translation helpful? Give feedback.
5 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Hello,
I moved to vite to build my React app and so on moved to vitest. I have now to make my tests run but I'm facing some issues when mocking fetch with msw.
I don't think it's an issue, I guess it's a misusage from me.
Vitest is configured as followed:
When I run my test (I'm running them against node@16 and FYI, I've tested it with node@18 but I have other errors), it ends with:
ReferenceError: Request is not defined
One of my tests:
The failing line:
When I run tests either with
happy-dom
, it uses fetch from happy-dom which relies on string and not on URL obect.I've tried to update to node@18 reading this discussion but it fails with
Error: connect ECONNREFUSED ::1:80
Beta Was this translation helpful? Give feedback.
All reactions