diff --git a/jest-puppeteer.config.js b/jest-puppeteer.config.js index 08f3a6ef..bb2fea42 100644 --- a/jest-puppeteer.config.js +++ b/jest-puppeteer.config.js @@ -9,9 +9,9 @@ module.exports = { headless: process.env.CI === 'true', }, browserContext: process.env.INCOGNITO ? 'incognito' : 'default', - server: { + server: [{ command: `PORT=${port} node server`, port, launchTimeout: 4000, - }, + }], } diff --git a/packages/jest-dev-server/README.md b/packages/jest-dev-server/README.md index 666a6803..fed0e3c9 100644 --- a/packages/jest-dev-server/README.md +++ b/packages/jest-dev-server/README.md @@ -17,11 +17,11 @@ This package extracts just the local development server spawning without any tie const { setup: setupDevServer } = require('jest-dev-server') module.exports = async function globalSetup() { - await setupDevServer({ + await setupDevServer([{ command: `node config/start.js --port=3000`, launchTimeout: 50000, port: 3000, - }) + }]) // Your global setup } ``` diff --git a/packages/jest-dev-server/src/global.js b/packages/jest-dev-server/src/global.js index 0a683d46..aafcae8a 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( @@ -63,7 +63,7 @@ function runServer(config = {}) { ) } - server = spawnd(config.command, { + servers[config.port] = 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[config.port].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 config = Array.isArray(providedConfigs) ? providedConfigs : [providedConfigs] + await Promise.all( + config.map(providedConfig => setupJestServer(providedConfig)) + ) +} + +export async function setupJestServer(providedConfig) { const config = { ...DEFAULT_CONFIG, ...providedConfig } const usedPortHandlers = { @@ -197,5 +205,8 @@ export async function setup(providedConfig) { } export async function teardown() { - if (server) await server.destroy() + const serverPortArray = Object.keys(servers) + if (serverPortArray.length) { + await Promise.all(serverPortArray.map(serverPort => servers[serverPort].destroy())) + } }