-
-
Notifications
You must be signed in to change notification settings - Fork 133
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
fix(ClientRequest): prevent stacked proxies when intercepting node http(s) requests #697
base: main
Are you sure you want to change the base?
Conversation
…ing node http(s) requests It was possible to end up with the http.request (etc) proxies wrapping existing proxies, which can cause odd behaviour.
Add logging so we know when we update an existing proxy and fix some linting (remove semicolons)
How does the code pass the check for an already applied interceptor? It seems like this is the problem we need to solve. |
I'm doing some more digging, but the core problem/discrepancy seems to come from this scenario:
How we end up with |
@sam-super, I think Jest contributes to this by creating a test sandbox. It may wipe out |
@mikicho yup just tested it and |
So MSW's attempt to use Because the Ideally maybe jest should reset the native-module cache between each test to remove any state that is attached (i.e. the interceptor proxy). Not sure how feasible it will be though. From my testing jest's own I know this PR isn't ideal, but i think preventing the stacking of proxies is probably still a good thing, and if MSW errors on detecting the proxy, it might break a lot of code that uses MSW in node/jest, so updating the proxy to the latest seems the least-bad way IMO. |
Hi, @sam-super. Thanks for proposing this. I would like to start from the beginning and have an isolated failing test for this scenario. Ideally, without using Jest. This bit concerns me:
If your environment gets
Do you have a test to prove this? MSW has been used with Jest for years, I don't believe this has been a problem. The root cause may be elsewhere for all I can see. MSW applies itself per process. Process cannot really change its globals. If it does, that's an unexpected behavior in whichever agent makes it so. So, if we can reproduce this in isolation without Jest, I am up to discussing the fix. If not, I will treat this as a Jest-specific shenanigan we won't cover. MSW doesn't officially support Jest since last October. |
@kettanaito I investigated this. I'm pretty sure it's because |
@mikicho, thanks for an update. And that happens while the test runs? Have you found the reasoning behind this behavior? |
It's |
I haven't needed to set |
@mikicho @kettanaito this is a repo i put together to demonstrate how jest resets global state and how the proxy MSW creates persists regardless: |
You run one worker, it's practically the same. in both cases, |
It's true in this specific case, and i have set a single worker to demonstrate the issue, but because jest re-uses worker threads, i don't think you need to set |
I didn't think the example repo (https://github.com/sam-super/msw-proxy-bug) was clear enough in what it was doing (due to the tests passing). I updated it so the tests fail in an attempted to make it clearer what is going on. It now has an assertion based on a few incorrect assumption of global state in jest and shows how it causes MSW to create multiple proxies: |
It was possible to end up with the http(s).request/get proxies wrapping existing proxies, which can cause odd behaviour:
nock/nock#2802
This PR attempts to fix the issue by only creating a proxy once, and just updating the callbacks the proxy uses when it's called a second time. it's not idea (since it could cause confusion with the previous instance, which will now no longer be invoked), but it seems like a preferable scenario.
instead we could error when an existing proxy is detected, but it seems like it would cause a lot of errors in practice (due to nodes module loader cache being inconsistent).