Skip to content

Commit

Permalink
feat(jest-dev-server): support multiple servers (#163)
Browse files Browse the repository at this point in the history
  • Loading branch information
xiaoyuhen authored and gregberge committed Dec 6, 2018
1 parent 7afbb2e commit 6cf690c
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 19 deletions.
49 changes: 37 additions & 12 deletions packages/jest-dev-server/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,12 @@ Starts a server before your Jest tests and tears it down after.
It's also useful for starting a local development server during the tests without letting Jest hang.
This package extracts just the local development server spawning without any ties to Puppeteer.

## Install

```bash
npm install --save-dev jest-dev-server
```

## Usage

`jest-dev-server` exports `setup` and `teardown` functions.
Expand All @@ -26,6 +32,27 @@ module.exports = async function globalSetup() {
}
```

It is also possible to specify several servers:

```js
// global-setup.js
const { setup: setupDevServer } = require('jest-dev-server')

module.exports = async function globalSetup() {
await setupDevServer([
{
command: 'node server.js',
port: 4444,
},
{
command: 'node server2.js',
port: 4445,
},
])
// Your global setup
}
```

```js
// global-teardown.js
const { teardown: teardownDevServer } = require('jest-dev-server')
Expand All @@ -36,9 +63,9 @@ module.exports = async function globalTeardown() {
}
```

#### Options
## Options

#### `command`
### `command`

Type: `string`, required.

Expand All @@ -51,7 +78,7 @@ module.exports = {
}
```

#### `debug`
### `debug`

Type: `boolean`, default to `false`.

Expand All @@ -64,7 +91,7 @@ module.exports = {
}
```

#### `launchTimeout`
### `launchTimeout`

Type: `number`, default to `5000`.

Expand All @@ -78,13 +105,11 @@ module.exports = {
}
```

#### `options`

Type: `object`, default to `{}`.
---

Any other options to pass to [`spawnd`](https://www.npmjs.com/package/spawnd).
Following options are linked to [`spawnd`](https://www.npmjs.com/package/spawnd).

#### `host`
### `host`

Type: `string`, default to `localhost`.

Expand All @@ -99,7 +124,7 @@ module.exports = {
}
```

#### `protocol`
### `protocol`

Type: `string`, default to `null`.

Expand All @@ -114,7 +139,7 @@ module.exports = {
}
```

#### `port`
### `port`

Type: `number`, default to `null`.

Expand All @@ -128,7 +153,7 @@ module.exports = {
}
```

#### `usedPortAction`
### `usedPortAction`

Type: `string` (`ask`, `error`, `ignore`, `kill`) default to `ask`.

Expand Down
24 changes: 17 additions & 7 deletions packages/jest-dev-server/src/global.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ export class JestDevServerError extends Error {
}
}

let server
const servers = []

function logProcDetection(proc, port) {
console.log(
Expand All @@ -55,15 +55,15 @@ async function killProc(proc) {
console.log(chalk.green(`Successfully killed process ${proc.name}`))
}

function runServer(config = {}) {
function runServer(config = {}, index) {
if (!config.command) {
throw new JestDevServerError(
'You must define a `command`',
ERROR_NO_COMMAND,
)
}

server = spawnd(config.command, {
servers[index] = spawnd(config.command, {
shell: true,
env: process.env,
cwd: cwd(),
Expand All @@ -73,7 +73,7 @@ function runServer(config = {}) {
if (config.debug) {
// eslint-disable-next-line no-console
console.log(chalk.magentaBright('\nJest dev-server output:'))
server.stdout.pipe(serverLogPrefixer).pipe(process.stdout)
servers[index].stdout.pipe(serverLogPrefixer).pipe(process.stdout)
}
}

Expand Down Expand Up @@ -103,7 +103,15 @@ function getIsPortTaken(port) {
})
}

export async function setup(providedConfig) {
export async function setup(providedConfigs) {
// Compatible with older versions
const configs = Array.isArray(providedConfigs) ? providedConfigs : [providedConfigs]
await Promise.all(
configs.map((providedConfig, index) => setupJestServer(providedConfig, index))
)
}

export async function setupJestServer(providedConfig, index) {
const config = { ...DEFAULT_CONFIG, ...providedConfig }

const usedPortHandlers = {
Expand Down Expand Up @@ -166,7 +174,7 @@ export async function setup(providedConfig) {
}
}

runServer(config)
runServer(config, index)

if (config.port) {
const { launchTimeout } = config
Expand Down Expand Up @@ -197,5 +205,7 @@ export async function setup(providedConfig) {
}

export async function teardown() {
if (server) await server.destroy()
if (servers.length) {
await Promise.all(servers.map(server => server.destroy()))
}
}

0 comments on commit 6cf690c

Please sign in to comment.