From 3dd4614adcdbb26e3cdee3ec2064d29bdfea9732 Mon Sep 17 00:00:00 2001 From: xiaoyuhen Date: Sat, 8 Dec 2018 01:47:13 +0800 Subject: [PATCH 1/2] feat: expose servers --- packages/jest-dev-server/README.md | 19 ++++++++++++++++++- packages/jest-dev-server/index.js | 1 + packages/jest-dev-server/src/global.js | 6 +++++- 3 files changed, 24 insertions(+), 2 deletions(-) diff --git a/packages/jest-dev-server/README.md b/packages/jest-dev-server/README.md index 8358e32b..65adbfb5 100644 --- a/packages/jest-dev-server/README.md +++ b/packages/jest-dev-server/README.md @@ -16,7 +16,7 @@ npm install --save-dev jest-dev-server ## Usage -`jest-dev-server` exports `setup` and `teardown` functions. +`jest-dev-server` exports `setup`,`teardown` and `getServers` functions. ```js // global-setup.js @@ -53,6 +53,23 @@ module.exports = async function globalSetup() { } ``` +```js +// global-setup.js +const { setup: setupDevServer, getServers} = require('jest-dev-server') + +module.exports = async function globalSetup() { + await setupDevServer({ + command: `node config/start.js --port=3000`, + launchTimeout: 50000, + port: 3000, + }) + getServers.then(servers => { + // You can get to the servers and do whatever you want + }) + // Your global setup +} +``` + ```js // global-teardown.js const { teardown: teardownDevServer } = require('jest-dev-server') diff --git a/packages/jest-dev-server/index.js b/packages/jest-dev-server/index.js index 16315eb0..57aebbd7 100644 --- a/packages/jest-dev-server/index.js +++ b/packages/jest-dev-server/index.js @@ -1,4 +1,5 @@ module.exports.setup = require('./lib/global').setup +module.exports.getServers = require('./lib/global').getServers module.exports.teardown = require('./lib/global').teardown module.exports.ERROR_TIMEOUT = require('./lib/global').ERROR_TIMEOUT module.exports.ERROR_PORT_USED = require('./lib/global').ERROR_PORT_USED diff --git a/packages/jest-dev-server/src/global.js b/packages/jest-dev-server/src/global.js index e939a4df..cd559541 100644 --- a/packages/jest-dev-server/src/global.js +++ b/packages/jest-dev-server/src/global.js @@ -111,7 +111,7 @@ export async function setup(providedConfigs) { ) } -export async function setupJestServer(providedConfig, index) { +async function setupJestServer(providedConfig, index) { const config = { ...DEFAULT_CONFIG, ...providedConfig } const usedPortHandlers = { @@ -204,6 +204,10 @@ export async function setupJestServer(providedConfig, index) { } } +export async function getServers() { + return servers +} + export async function teardown() { if (servers.length) { await Promise.all(servers.map(server => server.destroy())) From 1948a9b2c832983fe348a05890da2f6b79c3d94f Mon Sep 17 00:00:00 2001 From: xiaoyuhen Date: Mon, 10 Dec 2018 18:28:18 +0800 Subject: [PATCH 2/2] fix: remove async declaration --- packages/jest-dev-server/src/global.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/jest-dev-server/src/global.js b/packages/jest-dev-server/src/global.js index cd559541..0cd8290b 100644 --- a/packages/jest-dev-server/src/global.js +++ b/packages/jest-dev-server/src/global.js @@ -204,7 +204,7 @@ async function setupJestServer(providedConfig, index) { } } -export async function getServers() { +export function getServers() { return servers }