Skip to content

Commit

Permalink
fix: jest-dev-server can't detect used ports (#235)
Browse files Browse the repository at this point in the history
  • Loading branch information
xiaoyuhen authored May 1, 2019
1 parent f772d74 commit 8b64c10
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 21 deletions.
4 changes: 2 additions & 2 deletions packages/jest-dev-server/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
36 changes: 17 additions & 19 deletions packages/jest-dev-server/src/global.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ const DEFAULT_CONFIG = {
launchTimeout: 5000,
host: 'localhost',
port: null,
protocol: 'http',
protocol: 'tcp',
usedPortAction: 'ask',
waitOnScheme: {},
}
Expand Down Expand Up @@ -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,
)
}
}
}

Expand Down

0 comments on commit 8b64c10

Please sign in to comment.