start-server-and-test
Advanced tools
Comparing version 1.10.11 to 1.11.0
{ | ||
"name": "start-server-and-test", | ||
"description": "Starts server, waits for URL, then runs test command; when the tests end, shuts down server", | ||
"version": "1.10.11", | ||
"version": "1.11.0", | ||
"author": "Gleb Bahmutov <gleb.bahmutov@gmail.com>", | ||
@@ -68,3 +68,3 @@ "bugs": "https://github.com/bahmutov/start-server-and-test/issues", | ||
"test": "npm run unit", | ||
"unit": "mocha src/*-spec.js", | ||
"unit": "mocha test/helper src/*-spec.js", | ||
"unused-deps": "dependency-check --unused --no-dev .", | ||
@@ -81,3 +81,4 @@ "semantic-release": "semantic-release", | ||
"test4": "curl --insecure https://127.0.0.1:9000", | ||
"demo": "node src/bin/start.js http://127.0.0.1:9000", | ||
"message": "echo Hi there 👋", | ||
"demo": "node src/bin/start.js http://127.0.0.1:9000 message", | ||
"demo2": "node src/bin/start.js start http://127.0.0.1:9000 test2", | ||
@@ -95,2 +96,3 @@ "demo3": "node src/bin/start.js start-with-child http://127.0.0.1:9000 test", | ||
"demo-commands": "node src/bin/start.js 'node test/server.js --port 8800' 8800 'node test/client --port 8800'", | ||
"demo-multiple": "node src/bin/start.js 'node test/server --port 6000' 6000 'node test/server --port 6010' 6010 'curl http://127.0.0.1:6000 && curl http://127.0.0.1:6010'", | ||
"travis-deploy-once": "travis-deploy-once" | ||
@@ -100,2 +102,3 @@ }, | ||
"ban-sensitive-files": "1.9.7", | ||
"chai": "4.2.0", | ||
"cross-env": "7.0.2", | ||
@@ -109,3 +112,3 @@ "dependency-check": "3.4.1", | ||
"minimist": "1.2.5", | ||
"mocha": "7.1.0", | ||
"mocha": "7.1.1", | ||
"pre-git": "3.17.1", | ||
@@ -115,2 +118,4 @@ "prettier-standard": "8.0.1", | ||
"simple-commit-message": "4.0.13", | ||
"sinon": "9.0.2", | ||
"sinon-chai": "3.5.0", | ||
"snap-shot-it": "6.3.5", | ||
@@ -139,3 +144,9 @@ "standard": "13.1.0", | ||
} | ||
}, | ||
"standard": { | ||
"globals": [ | ||
"sandbox", | ||
"expect" | ||
] | ||
} | ||
} |
@@ -219,4 +219,10 @@ # start-server-and-test | ||
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 | ||
Sometimes you need to start one API server and one webserver in order to test the application. Use the syntax: | ||
``` | ||
start-test <first command> <first resource> <second command> <second resource> <test command> | ||
``` | ||
For example if API runs at port 3000 and server runs at port 8080: | ||
```json | ||
@@ -228,4 +234,3 @@ { | ||
"start:server": "node src/server", | ||
"start:server-and-api": "start-test start:api 7600 start:server", | ||
"test:all": "start-test start:server-and-api 5000 test" | ||
"test:all": "start-test start:api 3000 start:server 8080 test" | ||
} | ||
@@ -235,3 +240,3 @@ } | ||
In the above example you would run `npm run test:all` to start both servers and run the test. See repo [start-two-servers-example](https://github.com/bahmutov/start-two-servers-example) for full example | ||
In the above example you would run `npm run test:all` to start the API first, then when it responds, start the server, and when the server is responding, it would run the tests. After the tests finish, it will shut down both servers. See the repo [start-two-servers-example](https://github.com/bahmutov/start-two-servers-example) for full example | ||
@@ -238,0 +243,0 @@ ### Small print |
@@ -6,3 +6,3 @@ #!/usr/bin/env node | ||
const args = process.argv.slice(2) | ||
const startAndTest = require('..') | ||
const startAndTest = require('..').startAndTest | ||
const utils = require('../utils') | ||
@@ -13,11 +13,13 @@ | ||
debug('parsed args: %o', parsed) | ||
const { start, test, url } = parsed | ||
console.log('starting server using command "%s"', start) | ||
console.log('and when url "%s" is responding with HTTP status code 200', url) | ||
console.log('running tests using command "%s"', test) | ||
const { services, test } = parsed | ||
if (!Array.isArray(services)) { | ||
throw new Error(`Could not parse arguments %o, got %o`, args, parsed) | ||
} | ||
startAndTest({ start, url, test }).catch(e => { | ||
utils.printArguments({ services, test }) | ||
startAndTest({ services, test }).catch(e => { | ||
console.error(e) | ||
process.exit(1) | ||
}) |
@@ -0,1 +1,2 @@ | ||
// @ts-check | ||
'use strict' | ||
@@ -24,5 +25,5 @@ | ||
function startAndTest ({ start, url, test }) { | ||
function waitAndRun ({ start, url, runFn }) { | ||
la(is.unemptyString(start), 'missing start script name', start) | ||
la(is.unemptyString(test), 'missing test script name', test) | ||
la(is.fn(runFn), 'missing test script name', runFn) | ||
la( | ||
@@ -98,13 +99,44 @@ is.unemptyString(url) || is.unemptyArray(url), | ||
function runTests () { | ||
debug('running test script command: %s', test) | ||
return execa(test, { shell: true, stdio: 'inherit' }) | ||
} | ||
return waited | ||
.tapCatch(stopServer) | ||
.then(runTests) | ||
.then(runFn) | ||
.finally(stopServer) | ||
} | ||
module.exports = startAndTest | ||
const runTheTests = testCommand => () => { | ||
debug('running test script command: %s', testCommand) | ||
return execa(testCommand, { shell: true, stdio: 'inherit' }) | ||
} | ||
/** | ||
* Starts a single service and runs tests or recursively | ||
* runs a service, then goes to the next list, until it reaches 1 service and runs test. | ||
*/ | ||
function startAndTest ({ services, test }) { | ||
if (services.length === 0) { | ||
throw new Error('Got zero services to start ...') | ||
} | ||
if (services.length === 1) { | ||
const runTests = runTheTests(test) | ||
debug('single service "%s" to run and test', services[0].start) | ||
return waitAndRun({ | ||
start: services[0].start, | ||
url: services[0].url, | ||
runFn: runTests | ||
}) | ||
} | ||
return waitAndRun({ | ||
start: services[0].start, | ||
url: services[0].url, | ||
runFn: () => { | ||
debug('previous service started, now going to the next one') | ||
return startAndTest({ services: services.slice(1), test }) | ||
} | ||
}) | ||
} | ||
module.exports = { | ||
startAndTest | ||
} |
@@ -13,5 +13,9 @@ const la = require('lazy-ass') | ||
let start = 'start' | ||
const service = { | ||
start: 'start', | ||
url: undefined | ||
} | ||
const services = [service] | ||
let test = 'test' | ||
let url | ||
@@ -21,3 +25,3 @@ if (cliArgs.length === 1 && isUrlOrPort(cliArgs[0])) { | ||
// "start": "http://localhost:8080" | ||
url = normalizeUrl(cliArgs[0]) | ||
service.url = normalizeUrl(cliArgs[0]) | ||
} else if (cliArgs.length === 2) { | ||
@@ -27,3 +31,3 @@ if (isUrlOrPort(cliArgs[0])) { | ||
// like ":8080 test-ci" | ||
url = normalizeUrl(cliArgs[0]) | ||
service.url = normalizeUrl(cliArgs[0]) | ||
test = cliArgs[1] | ||
@@ -34,5 +38,16 @@ } | ||
// like "start-server 8080" | ||
start = cliArgs[0] | ||
url = normalizeUrl(cliArgs[1]) | ||
service.start = cliArgs[0] | ||
service.url = normalizeUrl(cliArgs[1]) | ||
} | ||
} else if (cliArgs.length === 5) { | ||
service.start = cliArgs[0] | ||
service.url = normalizeUrl(cliArgs[1]) | ||
const secondService = { | ||
start: cliArgs[2], | ||
url: normalizeUrl(cliArgs[3]) | ||
} | ||
services.push(secondService) | ||
test = cliArgs[4] | ||
} else { | ||
@@ -45,18 +60,15 @@ la( | ||
) | ||
start = cliArgs[0] | ||
url = normalizeUrl(cliArgs[1]) | ||
service.start = cliArgs[0] | ||
service.url = normalizeUrl(cliArgs[1]) | ||
test = cliArgs[2] | ||
} | ||
if (isPackageScriptName(start)) { | ||
start = `npm run ${start}` | ||
} | ||
services.forEach(service => { | ||
service.start = normalizeCommand(service.start) | ||
}) | ||
if (isPackageScriptName(test)) { | ||
test = `npm run ${test}` | ||
} | ||
test = normalizeCommand(test) | ||
return { | ||
start, | ||
url, | ||
services, | ||
test | ||
@@ -66,2 +78,6 @@ } | ||
function normalizeCommand (command) { | ||
return UTILS.isPackageScriptName(command) ? `npm run ${command}` : command | ||
} | ||
/** | ||
@@ -139,7 +155,25 @@ * Returns true if the given string is a name of a script in the package.json file | ||
module.exports = { | ||
function printArguments ({ services, test }) { | ||
services.forEach((service, k) => { | ||
console.log('%d: starting server using command "%s"', k + 1, service.start) | ||
console.log( | ||
'and when url "%s" is responding with HTTP status code 200', | ||
service.url | ||
) | ||
}) | ||
console.log('running tests using command "%s"', test) | ||
console.log('') | ||
} | ||
// placing functions into a common object | ||
// makes them methods for easy stubbing | ||
const UTILS = { | ||
getArguments, | ||
isPackageScriptName, | ||
isUrlOrPort, | ||
normalizeUrl | ||
normalizeUrl, | ||
printArguments | ||
} | ||
module.exports = UTILS |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
22576
285
288
20