Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 56382c4

Browse files
committedAug 30, 2024·
fix native fetch error in server-test on node 18
1 parent 5d63c99 commit 56382c4

File tree

2 files changed

+13
-2
lines changed

2 files changed

+13
-2
lines changed
 

‎package.json

+1
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@
5050
"clean-all": "rm -rf ./node_modules && rm -rf ./packages/*/node_modules && yarn run build-clean",
5151
"lint-fix": "eslint . --fix --cache",
5252
"lint": "eslint . --cache",
53+
"preinstall": "node -v",
5354
"postpublish": "yarn workspaces run cleanup-release",
5455
"publish": "yarn run build-clean && yarn run build && yarn workspaces run prepare-release && npm publish --workspaces",
5556
"start": "node packages/metro/src/cli",

‎packages/metro/src/integration_tests/__tests__/server-test.js

+12-2
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,18 @@ jest.unmock('cosmiconfig');
1919

2020
jest.setTimeout(60 * 1000);
2121

22+
// Can't set the "Connection" header in node < 18.14.1 (undici < 5.15.0): https://github.com/nodejs/undici/pull/1829,
23+
// However in these versions "Connection" is set to "close" by default in node 18 anyway comparing with later version
24+
const [nodeVersionMajor, nodeVersionMinor, nodeVersionPatch] =
25+
process.versions.node.split('.').map(Number);
26+
const canSetConnectionHeader =
27+
nodeVersionMajor > 18 || nodeVersionMinor > 14 || nodeVersionPatch >= 1;
28+
2229
// Workaround for https://github.com/nodejs/node/issues/54484:
2330
// Fetch with connection: close to prevent Node reusing connections across tests
2431
const fetchAndClose = (path: string) =>
2532
fetch(path, {
26-
headers: {Connection: 'close'},
33+
headers: canSetConnectionHeader ? {Connection: 'close'} : {},
2734
});
2835

2936
describe('Metro development server serves bundles via HTTP', () => {
@@ -34,7 +41,10 @@ describe('Metro development server serves bundles via HTTP', () => {
3441
async function downloadAndExec(path: string, context = {}): mixed {
3542
const response = await fetchAndClose(
3643
'http://localhost:' + config.server.port + path,
37-
);
44+
).catch(e => {
45+
console.error('Fetch failed', e);
46+
throw new Error('Fetch failed', {cause: e});
47+
});
3848
bundlesDownloaded.add(path);
3949

4050
const body = await response.text();

0 commit comments

Comments
 (0)
Please sign in to comment.