Repository navigation
Serve self-signed HTTPS fetches in dev-middleware tests through undici - #60362
Conversation
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>
chrfalch
left a comment
There was a problem hiding this comment.
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.
|
These landed in #60413, since this one merged first: the mock's response now has its status text, from |
Summary:
On Node 26, one dev-middleware test fails over HTTPS:
withFetchSelfSignedCertsForAllTestsspies on the globalfetchand passes it anAgentfrom theundicipackage (6.23.0) that accepts the test server's self-signed certificate. The globalfetchruns 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 withfetch(url, {dispatcher: new (require('undici').Agent)()}).This change answers the spied
fetchcalls with theundicipackage'srequest, which the other helpers inFetchUtils.jsalready use, and wraps the status and body text in aResponse. That response has no headers or status text. The code under test,Device.js, readsok,statusand the body, and putsstatusTextin an error message, where it is now empty. The mock also replaces anydispatcherpassed tofetchand does not follow redirects; these tests do neither.withFetchSelfSignedCertsForAllTestsloses itsfetchOriginalparameter.The
undicipackage's ownfetchis not used because it throwsmarkResourceTiming is not a functionunder Jest, whoseperformanceglobal lacks that method.Changelog:
[INTERNAL] [FIXED] - Fix the dev-middleware HTTPS tests on Node 26
Test Plan:
On Node 26.12.0:
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.