diff --git a/packages/jest-dev-server/README.md b/packages/jest-dev-server/README.md index 666a6803..8358e32b 100644 --- a/packages/jest-dev-server/README.md +++ b/packages/jest-dev-server/README.md @@ -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. @@ -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') @@ -36,9 +63,9 @@ module.exports = async function globalTeardown() { } ``` -#### Options +## Options -#### `command` +### `command` Type: `string`, required. @@ -51,7 +78,7 @@ module.exports = { } ``` -#### `debug` +### `debug` Type: `boolean`, default to `false`. @@ -64,7 +91,7 @@ module.exports = { } ``` -#### `launchTimeout` +### `launchTimeout` Type: `number`, default to `5000`. @@ -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`. @@ -99,7 +124,7 @@ module.exports = { } ``` -#### `protocol` +### `protocol` Type: `string`, default to `null`. @@ -114,7 +139,7 @@ module.exports = { } ``` -#### `port` +### `port` Type: `number`, default to `null`. @@ -128,7 +153,7 @@ module.exports = { } ``` -#### `usedPortAction` +### `usedPortAction` Type: `string` (`ask`, `error`, `ignore`, `kill`) default to `ask`. diff --git a/packages/jest-dev-server/src/global.js b/packages/jest-dev-server/src/global.js index 0a683d46..e939a4df 100644 --- a/packages/jest-dev-server/src/global.js +++ b/packages/jest-dev-server/src/global.js @@ -39,7 +39,7 @@ export class JestDevServerError extends Error { } } -let server +const servers = [] function logProcDetection(proc, port) { console.log( @@ -55,7 +55,7 @@ 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`', @@ -63,7 +63,7 @@ function runServer(config = {}) { ) } - server = spawnd(config.command, { + servers[index] = spawnd(config.command, { shell: true, env: process.env, cwd: cwd(), @@ -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) } } @@ -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 = { @@ -166,7 +174,7 @@ export async function setup(providedConfig) { } } - runServer(config) + runServer(config, index) if (config.port) { const { launchTimeout } = config @@ -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())) + } }