Skip to content

Serve self-signed HTTPS fetches in dev-middleware tests through undici - #60362

Merged
huntie merged 1 commit into
react:mainfrom
expo:@ide/dev-middleware-self-signed-fetch
Nov 30, 2026
Merged

huntie merged 1 commit into
react:mainfrom
expo:@ide/dev-middleware-self-signed-fetch

Conversation

@ide

@ide ide commented Nov 28, 2026 •

Copy link
Copy Markdown
Contributor

Summary:

On Node 26, one dev-middleware test fails over HTTPS:

inspector proxy CDP rewriting hacks over HTTPS › Debugger.getScriptSource › fetches source from server
  - "scriptSource": "foo",
  + "error": { "message": "Failed to fetch source url \"https://localhost:…/source\" for scriptId script1: fetch failed" },

withFetchSelfSignedCertsForAllTests spies on the global fetch and passes it an Agent from the undici package (6.23.0) that accepts the test server's self-signed certificate. The global fetch runs on the undici bundled with Node, which is 8.x on Node 26. It calls the dispatcher with a handler interface that undici 6 rejects: InvalidArgumentError: invalid onError method. This reproduces outside Jest with fetch(url, {dispatcher: new (require('undici').Agent)()}).

This change answers the spied fetch calls with the undici package's request, which the other helpers in FetchUtils.js already use, and wraps the status and body text in a Response. That response has no headers or status text. The code under test, Device.js, reads ok, status and the body, and puts statusText in an error message, where it is now empty. The mock also replaces any dispatcher passed to fetch and does not follow redirects; these tests do neither. withFetchSelfSignedCertsForAllTests loses its fetchOriginal parameter.

The undici package's own fetch is not used because it throws markResourceTiming is not a function under Jest, whose performance global lacks that method.

Changelog:

[INTERNAL] [FIXED] - Fix the dev-middleware HTTPS tests on Node 26

Test Plan:

On Node 26.12.0:

yarn jest --runInBand packages/dev-middleware

On main, "fetches source from server" fails over HTTPS as above; on this branch the suite passes. Flow, ESLint and Prettier pass. Not run: Node 22 and 24.

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 <noreply@anthropic.com>
@meta-cla meta-cla Bot added the CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. label Nov 28, 2026
@ide
ide marked this pull request as ready for review November 29, 2026 12:02
@facebook-github-tools facebook-github-tools Bot added the Shared with Meta Applied via automation to indicate that an Issue or Pull Request has been shared with the team. label Nov 29, 2026

@chrfalch chrfalch left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good, can be approved. It works on every Node version in engines (22.13 and up). A few non-blocking notes:

1. statusText is lost. The mock in src/__tests__/FetchUtils.js builds new Response(text, {status}) without statusText or headers. Device.js:1106 builds its error from response.status + ' ' + response.statusText, so over HTTPS that message would read HTTP 404 with nothing after it. No current test reaches that path, but passing statusText through (undici's request doesn't return it, so it would come from http.STATUS_CODES[statusCode]) would keep the mock faithful.

2. The mock is narrower than fetch. new Response(body, {status: 204 or 304}) throws, because those statuses must not have a body. request doesn't follow redirects, and url.toString() breaks if a Request object is passed. Today only fetch(url) with a URL is used, so nothing breaks; a short comment on these limits would help whoever extends the tests next.

3. The HTTPS run no longer exercises Node's fetch. It now tests undici's request behind a fetch-shaped wrapper. That's fine for a test-only fix, but worth a line in the description, so it's clear that HTTPS fetch itself isn't covered anymore.

@huntie
huntie merged commit 84a4821 into react:main Nov 30, 2026
108 of 110 checks passed
@huntie
huntie deleted the @ide/dev-middleware-self-signed-fetch branch November 30, 2026 12:45
@ide

ide commented Nov 30, 2026

Copy link
Copy Markdown
Contributor Author

These landed in #60413, since this one merged first: the mock's response now has its status text, from http.STATUS_CODES, and a comment states what the mock leaves out (redirects, Request objects, headers, and a 204 or 304 response, which throws). Its description says the HTTPS tests now run undici's request, not Node's fetch.

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

Labels

CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. Shared with Meta Applied via automation to indicate that an Issue or Pull Request has been shared with the team.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants