Skip to content

fix(mock): native fetch bypasses MockAgent due to allowH2#5448

Open
Megashubham wants to merge 2 commits into
nodejs:mainfrom
Megashubham:fix-mock-agent-fetch-issue-5036
Open

fix(mock): native fetch bypasses MockAgent due to allowH2#5448
Megashubham wants to merge 2 commits into
nodejs:mainfrom
Megashubham:fix-mock-agent-fetch-issue-5036

Conversation

@Megashubham

@Megashubham Megashubham commented Jun 22, 2026

Copy link
Copy Markdown

Fixes #5036

Description

This PR fixes an issue where native Node.js fetch queries would bypass the MockAgent globally registered using setGlobalDispatcher.

Root Cause

In version 8.0.3, a change was introduced to wrap the legacy global dispatcher with a Dispatcher1Wrapper (fix: mirror the legacy global dispatcher for built-in fetch). To support legacy consumers, Dispatcher1Wrapper.dispatch modifies opts to forcefully add allowH2: false.

Because of this, native node fetch passes allowH2: false when delegating to the MockAgent. When MockAgent proxies the request to its internal encapsulated Agent instance:

  1. MockAgent.get(origin) pre-registers and creates a MockClient for the origin exactly as-is.
  2. The encapsulated Agent.dispatch checks allowH2: false and modifies the lookup key to be ${origin}#http1-only.
  3. Because the MockClient registered is under ${origin}, the cache miss causes the underlying Agent to instantiate a brand new real Client under ${origin}#http1-only.
  4. As a result, the request completely bypasses the mock interceptors and is fired over the real network!

Fix

The mock interceptor system shouldn't be concerned about whether HTTP/2 or HTTP/1.1 is being explicitly requested by the wrapper. To solve this, MockAgent.dispatch now safely deletes the allowH2 flag from dispatchOpts if it is set to false. This ensures the underlying encapsulated Agent retains the standard origin as the lookup key and successfully resolves to the pre-registered MockClient.

A regression test using native globalThis.fetch has also been added in test/issue-5036.js.

Comment thread lib/mock/mock-agent.js Outdated
@codecov-commenter

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 93.46%. Comparing base (0f1f890) to head (e9e49a0).
⚠️ Report is 1 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #5448      +/-   ##
==========================================
- Coverage   93.46%   93.46%   -0.01%     
==========================================
  Files         110      110              
  Lines       37106    37110       +4     
==========================================
+ Hits        34682    34685       +3     
- Misses       2424     2425       +1     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@Megashubham

Megashubham commented Jun 23, 2026

Copy link
Copy Markdown
Author

Can you check once :P
@metcoder95

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

MockAgent as global dispatcher broken since version 8.0.3

3 participants