Comments (8)
I have a hard requirement to import fetch directly from the undici package.
That shouldn't be a problem.
Also, the node built-in fetch does not allow passing a dispatcher directly, unlike fetch from undici.
TIL!
No worries, let's reopen this then.
from msw.
@TheHolyWaffle we have exactly the same reasons to use undici
instead of the Node's native fetch
: the dispatcher
. And the same problem, that MSW does not intercept and mock the requests, but let's the requests go through to the real service.
Have you found a decent solution?
from msw.
@TheHolyWaffle Are you sure that node's fetch does not support the dispatcher field? I have the same problem, but as far as I understand, the issue is with TS types, not in the API of the native fetch. At least the typings for the "node version of the types" do include the dispatcher
field in the RequestInit
object.
DefinitelyTyped/DefinitelyTyped#66824
If you are using typescript, the @types/node
library may be giving you the DOM types when you'd prefer to use the types for node ("undici types"). If your TS project includes a frontend, you may have configured lib.dom.d.ts
in use yourself ("lib": ["dom"]
in tsconfig
) or a dependency of yours may be pulling it in (you can try to use explainFiles to find out the cause(s) of the files inclusion if its unclear).
This may be of no real help to you. Just jotting this down for future reference in case it has an impact on API decision. Still looking for a feasible workaround myself.
E: At least one workaround is to use multiple tsconfig
files and structure your project so that the fetch
integration is covered by a tsconfig
without lib.dom.d.ts
and any dependencies that may end up including it.
from msw.
Why this happens
Undici is a custom client that doesn't rely on the node:http
module, which is a primary request-issuing module which MSW observes in Node.js (as of now). Because of that, the requests Undici makes are invisible to MSW (I believe Undici uses net.Socket
directly).
How to solve this
For now, you can switch to Undici's MockAgent
as Undici ships with their own mocking in mind. If that works for you, that's great!
Note that you can call MSW request handlers directly, including in your
MockAgent
by Undici. See thegetResponse()
function.
Undici support
MSW has a strong position on not shipping request client-specific code. Instead, we try out best to support the underlying protocols, tapping into the network at the layer that is both more resilient and less intrusive to the things you are running.
I highly doubt a standalone Undici support will ever happen in MSW. What may happen, is net.Socket
support, which Undici uses. We've already merged the Socket-based interceptor in @mswjs/interceptors
but (a) it's not used in MSW yet; (b) it still taps into node:http
by using a custom Agent
and a mock socket.
Raw socket-level interception is theoretically possible, we just need to get it right. Sockets transport many things, not just HTTP requests. We better not interfere with those things.
Important
Also please note that MSW supports global fetch
in Node.js still. That is also, technically, Undici. It just has to be a global fetch
function. It's important to disambiguate that we aren't talking about global fetch
but about a standalone usage of Undici, like their request
function.
from msw.
@kettanaito Thanks for looking into this.
I have removed the polyfills again in the minimal reproduction. They were a futile attempt to fix my issue but they never made sense. This bug purely revolves around a nodejs backend microservice, no jsdom environment is needed.
I have a hard requirement to import fetch
directly from the undici
package. This is because the node built-in version of fetch
is from an older undici
version. Since then, there have been bugs fixed in undici
. Also, the node built-in fetch does not allow passing a dispatcher
directly, unlike fetch
from undici
. Because of these reasons, I cannot use the global fetch
.
Please reconsider this issue 🙏
from msw.
Hi, @TheHolyWaffle. Thanks for reporting this.
Please, compare your usage example to the Jest and Jest+JSDOM examples from MSW. Those are functional (as functional as anything on JSDOM can get).
You don't have to import fetch
from undici explicitly if you have this line.
I also highly recommend to drop jest.polyfills.js
and use jest-fixed-jsdom preset instead. Try that and let me know if you still encounter the issue.
from msw.
@bobanm I went ahead with Undici's MockAgent
instead of msw
, see https://undici.nodejs.org/#/docs/api/MockAgent
Feel free to 👍 the original issue to bring more attention to this!
from msw.
@TheHolyWaffle I ended up doing the same. I find it to be a little bit less elegant than using MSW, but still quite a good solution.
I hope MSW adds support for Undici.
from msw.
Related Issues (20)
- Support option to use customized script to `server.listen()` for consistency with `worker.start()`
- Can't match the handler in tests in nuxt 3 app using useFetch composable HOT 1
- Console log left over HOT 4
- Add cross platform path support to workerDirectory HOT 7
- not compatible with fetch-retry HOT 2
- MSW in fallback mode not intercepting and mocking image binary request HOT 5
- Document ListenOptions HOT 1
- Disabling "navigate" bypass causes TypeError: "Uncaught (in promise) TypeError: Failed to construct 'Request': Cannot construct a Request with a RequestInit whose mode member is set as 'navigate'."
- Requests with ".local" domain name take too long to resolve HOT 4
- Is it good practice to reference a video tutorial that is behind a pay wall? HOT 1
- Question: Types - should PathParams type accept optional values?
- 2.4.0 Main entry point still exports handlers (i.e: `GraphQLHandler`) HOT 5
- Vitest tests fail with error "Cannot find package 'graphql' imported" in v2.4.0 HOT 20
- Component with fetch call during onMount fails due some race condition where server.listen() doesn't kick in time HOT 2
- "TypeError: A dynamic import callback was invoked without --experimental-vm-modules" in Jest HOT 8
- Proposal: Mock RSC payload
- socket `connect` event is not emitted when sending multiple requests concurrently with a http agent `keepAlive: true` HOT 1
- XHR Upload events are not being triggered HOT 1
- Module not found: Error: Can't resolve 'graphql' HOT 1
- Node.js (Undici) fetch doesn't follow redirects when mocked with MSW HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from msw.