Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions flow-typed/npm/undici_v6.x.x.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ declare module 'undici' {
headers: Headers,
body: {
read(): Promise<Buffer>,
text(): Promise<string>,
...
},
...
Expand Down
24 changes: 13 additions & 11 deletions packages/dev-middleware/src/__tests__/FetchUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,12 +62,12 @@ export async function fetchJson<T extends JSONSerializable>(
}

/**
* 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<Parameters<typeof fetch>, ReturnType<typeof fetch>>,
fetchOriginal: typeof fetch,
) {
const selfSignedCertDispatcher = new Agent({
connect: {
Expand All @@ -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(() => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ jest.useRealTimers();

jest.setTimeout(10000);

const fetchOriginal = fetch;
const fetchSpy: JestMockFn<
Parameters<typeof fetch>,
ReturnType<typeof fetch>,
Expand All @@ -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({
Expand Down
Loading