Skip to content

Commit

Permalink
support multiple servers
Browse files Browse the repository at this point in the history
  • Loading branch information
xiaoyuhen committed Dec 2, 2018
1 parent 98cf634 commit 86552c3
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 9 deletions.
4 changes: 2 additions & 2 deletions jest-puppeteer.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
},
}],
}
4 changes: 2 additions & 2 deletions packages/jest-dev-server/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
```
Expand Down
21 changes: 16 additions & 5 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 @@ -63,7 +63,7 @@ function runServer(config = {}) {
)
}

server = spawnd(config.command, {
servers[config.port] = 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[config.port].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 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 = {
Expand Down Expand Up @@ -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()))
}
}

0 comments on commit 86552c3

Please sign in to comment.