-
-
Notifications
You must be signed in to change notification settings - Fork 537
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
msw/node unable to intercept requests from fetch in undici #2165
Comments
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 I also highly recommend to drop |
@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 Please reconsider this issue 🙏 |
That shouldn't be a problem.
TIL! No worries, let's reopen this then. |
@TheHolyWaffle we have exactly the same reasons to use Have you found a decent solution? |
@bobanm I went ahead with Undici's Feel free to 👍 the original issue to bring more attention to this! |
@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. |
@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 DefinitelyTyped/DefinitelyTyped#66824 If you are using typescript, the 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 |
Why this happensUndici is a custom client that doesn't rely on the How to solve thisFor now, you can switch to Undici's
Undici supportMSW 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 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 |
There's no plans to add raw Socket-level interception to support the direct usage of Undici. Please refer to my response above to see how you can fix it in your app. You can also expose Undici's fetch globally as a part of your setup and have the interception working. |
Prerequisites
Environment check
msw
versionNode.js version
20.13.1
Reproduction repository
https://github.com/TheHolyWaffle/msw-undici-reproduction
Reproduction steps
npm i
npx jest
to run the sampletest.js
unit testCurrent behavior
When adding a GET handler to
https://example.com
,msw
does not intercept the request. Instead thefetch
goes to the real example.com. See failing unit test.To make the unit test succeed, uncomment
const { fetch } = require("undici");
so that the node-native fetch is used instead of the `Expected behavior
msw
should intercept thefetch
call fromundici
and unit test should succeed.The text was updated successfully, but these errors were encountered: