Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat:support multiple servers #163

Merged
merged 5 commits into from
Dec 6, 2018
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions packages/jest-dev-server/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,23 @@ module.exports = async function globalSetup() {
}
```

`jest-dev-server` can also able to launch several servers

```js
module.exports = {
server: [
{
command: 'node server.js',
port: 4444,
},
{
command: 'node server2.js',
port: 4445,
},
]
}
```

```js
// global-teardown.js
const { teardown: teardownDevServer } = require('jest-dev-server')
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()))
}
}