diff --git a/flow-typed/npm/undici_v6.x.x.js b/flow-typed/npm/undici_v6.x.x.js index 9076fb91bee..3bf75ab2dec 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 78ddfe01d54..1bc4735c87f 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 17839c3c2ab..f09a4ac242e 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({