diff --git a/README.md b/README.md index 725c48e0..dde0e562 100644 --- a/README.md +++ b/README.md @@ -140,6 +140,10 @@ To see diagnostic messages, run with environment variable `DEBUG=start-server-an To see disable HTTPS checks for `wait-on`, run with environment variable `START_SERVER_AND_TEST_INSECURE=1`. +### Timeout + +This utility will wait for maximum of 5 minutes while checking for the server to respond. + ### Starting two servers Sometimes you need to start one API server and one webserver in order to test the application. Just have two commands cascade. First command should wait on the webserver script, which in turn uses `start-server-and-test` to start the API server before running the webserver. Something like this diff --git a/src/index.js b/src/index.js index 1de0b954..2a04a6f5 100644 --- a/src/index.js +++ b/src/index.js @@ -8,6 +8,11 @@ const Promise = require('bluebird') const psTree = require('ps-tree') const debug = require('debug')('start-server-and-test') +/** + * Used for timeout (ms) + */ +const fiveMinutes = 5 * 60 * 1000 + const isDebug = () => process.env.DEBUG && process.env.DEBUG.indexOf('start-server-and-test') !== -1 @@ -63,26 +68,27 @@ function startAndTest ({ start, url, test }) { server.on('close', onClose) debug('starting waitOn %s', url) - waitOn( - { - resources: Array.isArray(url) ? url : [url], - interval: 2000, - window: 1000, - verbose: isDebug(), - strictSSL: !isInsecure(), - log: isDebug() - }, - err => { - if (err) { - debug('error waiting for url', url) - debug(err.message) - return reject(err) - } - debug('waitOn finished successfully') - server.removeListener('close', onClose) - resolve() + const options = { + resources: Array.isArray(url) ? url : [url], + interval: 2000, + window: 1000, + timeout: fiveMinutes, + verbose: isDebug(), + strictSSL: !isInsecure(), + log: isDebug() + } + debug('wait-on options %o', options) + + waitOn(options, err => { + if (err) { + debug('error waiting for url', url) + debug(err.message) + return reject(err) } - ) + debug('waitOn finished successfully') + server.removeListener('close', onClose) + resolve() + }) }) function runTests () {