Skip to content

Commit

Permalink
fix native fetch error in server-test on node 18
Browse files Browse the repository at this point in the history
  • Loading branch information
vzaidman committed Aug 30, 2024
1 parent 5d63c99 commit f7dfb0c
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 2 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
14 changes: 12 additions & 2 deletions packages/metro/src/integration_tests/__tests__/server-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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', () => {
Expand All @@ -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();
Expand Down

0 comments on commit f7dfb0c

Please sign in to comment.