Skip to content

Commit

Permalink
feat: working wait
Browse files Browse the repository at this point in the history
  • Loading branch information
bahmutov committed Nov 7, 2017
1 parent 007c565 commit e3b8dd6
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 4 deletions.
15 changes: 12 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,10 @@
"test": "npm run unit",
"unit": "mocha src/*-spec.js",
"unused-deps": "dependency-check --unused --no-dev .",
"semantic-release": "semantic-release pre && npm publish && semantic-release post"
"semantic-release": "semantic-release pre && npm publish && semantic-release post",
"prestart": "sleep 5",
"start": "http-server -c-1 -p 8090 --quiet test",
"demo": "node bin/start.js http://127.0.0.1:8090"
},
"release": {
"analyzeCommits": "simple-commit-message",
Expand All @@ -94,10 +97,16 @@
"prettier-standard": "7.0.3",
"semantic-release": "8.2.0",
"simple-commit-message": "3.3.2",
"standard": "10.0.3"
"standard": "10.0.3",
"http-server": "0.10.0"
},
"dependencies": {
"check-more-types": "2.24.0",
"lazy-ass": "1.6.0"
"lazy-ass": "1.6.0",
"execa": "0.8.0",
"wait-on": "2.0.2",
"bluebird": "3.5.1",
"debug": "3.1.0"
}
}

43 changes: 42 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,53 @@

const la = require('lazy-ass')
const is = require('check-more-types')
const execa = require('execa')
const waitOn = require('wait-on')
const Promise = require('bluebird')
const debug = require('debug')('start-server-and-test')

function startAndTest ({ start, url, test }) {
la(is.unemptyString(start), 'missing start script name', start)
la(is.unemptyString(test), 'missing test script name', test)
la(is.url(url), 'missing url to wait on', url)
return Promise.resolve()

debug('starting server')
const server = execa('npm', ['run', start], { stdio: 'inherit' })
let serverStopped

function stopServer () {
if (!serverStopped) {
debug('stopping server')
server.kill()
serverStopped = true
}
}

const waited = new Promise((resolve, reject) => {
waitOn(
{
resources: [url]
},
err => {
if (err) {
debug('error waiting for url', url)
debug(err.message)
return reject(err)
}
resolve()
}
)
})

function runTests () {
debug('running test script command', test)
return execa('npm', ['run', test], { stdio: 'inherit' })
}

return waited
.tapCatch(stopServer)
.then(runTests)
.finally(stopServer)
}

module.exports = startAndTest

0 comments on commit e3b8dd6

Please sign in to comment.