From f02886042f7333300efb3d643ff9568466d1772a Mon Sep 17 00:00:00 2001 From: xiaoyuhen Date: Tue, 30 Apr 2019 16:29:23 +0800 Subject: [PATCH] fix: jest-dev-server can't detect used ports --- packages/jest-dev-server/README.md | 4 +-- packages/jest-dev-server/src/global.js | 36 ++++++++++++-------------- 2 files changed, 19 insertions(+), 21 deletions(-) diff --git a/packages/jest-dev-server/README.md b/packages/jest-dev-server/README.md index 595e35a2..77fd129f 100644 --- a/packages/jest-dev-server/README.md +++ b/packages/jest-dev-server/README.md @@ -143,9 +143,9 @@ module.exports = { ### `protocol` -Type: `string`, default to `null`. +Type: `string`, (`https`, `http`, `tcp`, `socket`) default to `tcp`. -To wait for an HTTP endpoint before considering the server running, include `http` as a protocol. +To wait for an HTTP or TCP endpoint before considering the server running, include `http` or `tcp` as a protocol. Must be used in conjunction with `port`. ```js diff --git a/packages/jest-dev-server/src/global.js b/packages/jest-dev-server/src/global.js index 355ae0e1..64b8f503 100644 --- a/packages/jest-dev-server/src/global.js +++ b/packages/jest-dev-server/src/global.js @@ -16,7 +16,7 @@ const DEFAULT_CONFIG = { launchTimeout: 5000, host: 'localhost', port: null, - protocol: 'http', + protocol: 'tcp', usedPortAction: 'ask', waitOnScheme: {}, } @@ -189,28 +189,26 @@ async function setupJestServer(providedConfig, index) { if (config.port) { const { launchTimeout, protocol, host, port, waitOnScheme } = config + let url = '' + if (protocol === 'tcp' || protocol === 'socket') { + url = `${protocol}:${host}:${port}` + } else { + url = `${protocol}://${host}:${port}` + } const opts = { - resources: [`${protocol}://${host}:${port}`], + resources: [url], + timeout: launchTimeout, ...waitOnScheme, } - let timeout - await Promise.race([ - new Promise((resolve, reject) => { - timeout = setTimeout( - () => - reject( - new JestDevServerError( - `Server has taken more than ${launchTimeout}ms to start.`, - ERROR_TIMEOUT, - ), - ), - launchTimeout, - ) - }), - waitOn(opts), - ]) - clearTimeout(timeout) + try { + await waitOn(opts) + } catch (err) { + throw new JestDevServerError( + `Server has taken more than ${launchTimeout}ms to start.`, + ERROR_TIMEOUT, + ) + } } }