From f7dfb0c31a535f610eb72dce4368b730e4c774b5 Mon Sep 17 00:00:00 2001 From: Vitali Zaidman Date: Fri, 30 Aug 2024 14:20:03 +0100 Subject: [PATCH] fix native fetch error in server-test on node 18 --- package.json | 1 + .../src/integration_tests/__tests__/server-test.js | 14 ++++++++++++-- 2 files changed, 13 insertions(+), 2 deletions(-) diff --git a/package.json b/package.json index a247158936..f05f3da4e7 100644 --- a/package.json +++ b/package.json @@ -50,6 +50,7 @@ "clean-all": "rm -rf ./node_modules && rm -rf ./packages/*/node_modules && yarn run build-clean", "lint-fix": "eslint . --fix --cache", "lint": "eslint . --cache", + "preinstall": "node -v", "postpublish": "yarn workspaces run cleanup-release", "publish": "yarn run build-clean && yarn run build && yarn workspaces run prepare-release && npm publish --workspaces", "start": "node packages/metro/src/cli", diff --git a/packages/metro/src/integration_tests/__tests__/server-test.js b/packages/metro/src/integration_tests/__tests__/server-test.js index 1156c3362e..b8cb069801 100644 --- a/packages/metro/src/integration_tests/__tests__/server-test.js +++ b/packages/metro/src/integration_tests/__tests__/server-test.js @@ -19,11 +19,18 @@ jest.unmock('cosmiconfig'); jest.setTimeout(60 * 1000); +// Can't set the "Connection" header in node < 18.14.1 (undici < 5.15.0): https://github.com/nodejs/undici/pull/1829, +// However in these versions "Connection" is set to "close" by default in node 18 anyway comparing with later version +const [nodeVersionMajor, nodeVersionMinor, nodeVersionPatch] = + process.versions.node.split('.').map(Number); +const canSetConnectionHeader = + nodeVersionMajor > 18 || nodeVersionMinor > 14 || nodeVersionPatch >= 1; + // Workaround for https://github.com/nodejs/node/issues/54484: // Fetch with connection: close to prevent Node reusing connections across tests const fetchAndClose = (path: string) => fetch(path, { - headers: {Connection: 'close'}, + headers: canSetConnectionHeader ? {Connection: 'close'} : {}, }); describe('Metro development server serves bundles via HTTP', () => { @@ -34,7 +41,10 @@ describe('Metro development server serves bundles via HTTP', () => { async function downloadAndExec(path: string, context = {}): mixed { const response = await fetchAndClose( 'http://localhost:' + config.server.port + path, - ); + ).catch(e => { + console.error('Fetch failed', e); + throw new Error('Fetch failed', {cause: e}); + }); bundlesDownloaded.add(path); const body = await response.text();