From b55a8f853ea54462e46309554b7f8d595a686bee Mon Sep 17 00:00:00 2001 From: James Ide Date: Sat, 28 Nov 2026 02:44:26 -0800 Subject: [PATCH] Serve self-signed HTTPS fetches in dev-middleware tests through undici On Node 26, InspectorProxyCdpRewritingHacks-test.js fails over HTTPS: "Debugger.getScriptSource fetches source from server" gets `fetch failed` instead of the script source. `withFetchSelfSignedCertsForAllTests` spies on the global `fetch` and passes it an `Agent` from the undici package (6.x) that accepts self-signed certificates. The global `fetch` runs on the undici bundled with Node, which is 8.x on Node 26 and calls the dispatcher with a handler interface that undici 6 rejects ("invalid onError method"). Answer the spied `fetch` calls with the undici package's `request`, which the other helpers in FetchUtils.js already use, and wrap the status and body text in a `Response`. The package's own `fetch` is not an option: it throws under Jest because the environment's `performance` has no `markResourceTiming`. The helper no longer calls the original `fetch`, so its `fetchOriginal` parameter is removed. Co-Authored-By: Claude Opus 5.5 --- flow-typed/npm/undici_v6.x.x.js | 1 + .../src/__tests__/FetchUtils.js | 24 ++++++++++--------- .../InspectorProxyCdpRewritingHacks-test.js | 3 +-- 3 files changed, 15 insertions(+), 13 deletions(-) diff --git a/flow-typed/npm/undici_v6.x.x.js b/flow-typed/npm/undici_v6.x.x.js index 9076fb91bee3..3bf75ab2dec7 100644 --- a/flow-typed/npm/undici_v6.x.x.js +++ b/flow-typed/npm/undici_v6.x.x.js @@ -38,6 +38,7 @@ declare module 'undici' { headers: Headers, body: { read(): Promise, + text(): Promise, ... }, ... diff --git a/packages/dev-middleware/src/__tests__/FetchUtils.js b/packages/dev-middleware/src/__tests__/FetchUtils.js index 78ddfe01d54b..1bc4735c87fb 100644 --- a/packages/dev-middleware/src/__tests__/FetchUtils.js +++ b/packages/dev-middleware/src/__tests__/FetchUtils.js @@ -62,12 +62,12 @@ export async function fetchJson( } /** - * Change the global fetch dispatcher to allow self-signed certificates. - * This runs with Jest's `beforeAll` and `afterAll`, and restores the original dispatcher. + * Serve the global `fetch` with undici's `request` so that it accepts the + * self-signed certificate of the HTTPS server created in ServerUtils. + * This runs with Jest's `beforeAll` and `afterAll`, and restores the original `fetch`. */ export function withFetchSelfSignedCertsForAllTests( fetchSpy: JestMockFn, ReturnType>, - fetchOriginal: typeof fetch, ) { const selfSignedCertDispatcher = new Agent({ connect: { @@ -76,15 +76,17 @@ export function withFetchSelfSignedCertsForAllTests( }); beforeAll(() => { - // For some reason, setting the `selfSignedCertDispatcher` with `setGlobalDispatcher` doesn't work. - // Instead of using `setGlobalDispatcher`, we'll use a spy to intercept the fetch calls and add the dispatcher. - fetchSpy.mockImplementation((url, options) => - fetchOriginal(url, { + // The global `fetch` runs on the undici bundled with Node, which may not + // accept a dispatcher from the undici package: Node 26 rejects one from + // undici 6. The package's own `fetch` throws under Jest, whose + // `performance` global lacks `markResourceTiming`. + fetchSpy.mockImplementation(async (url, options) => { + const {statusCode, body} = await request(url.toString(), { ...options, - // $FlowFixMe[prop-missing]: dispatcher - dispatcher: options?.dispatcher ?? selfSignedCertDispatcher, - }), - ); + dispatcher: selfSignedCertDispatcher, + }); + return new Response(await body.text(), {status: statusCode}); + }); }); afterAll(() => { diff --git a/packages/dev-middleware/src/__tests__/InspectorProxyCdpRewritingHacks-test.js b/packages/dev-middleware/src/__tests__/InspectorProxyCdpRewritingHacks-test.js index 17839c3c2ab7..f09a4ac242e7 100644 --- a/packages/dev-middleware/src/__tests__/InspectorProxyCdpRewritingHacks-test.js +++ b/packages/dev-middleware/src/__tests__/InspectorProxyCdpRewritingHacks-test.js @@ -28,7 +28,6 @@ jest.useRealTimers(); jest.setTimeout(10000); -const fetchOriginal = fetch; const fetchSpy: JestMockFn< Parameters, ReturnType, @@ -39,7 +38,7 @@ describe.each(['HTTP', 'HTTPS'])( protocol => { // Inspector proxy tests are using a self-signed certificate for HTTPS tests. if (protocol === 'HTTPS') { - withFetchSelfSignedCertsForAllTests(fetchSpy, fetchOriginal); + withFetchSelfSignedCertsForAllTests(fetchSpy); } const serverRef = withServerForEachTest({