Skip to content

Commit

Permalink
feat: start and wait for multiple services 248 (#249)
Browse files Browse the repository at this point in the history
* chore: start refactoring start and url parameters

* chore: refactor a little more

* chore: add sinon and global sandbox

* chore: add sinon

* chore: normalize all start commands

* lint

* chore: parse second service

* add another test

* move print into utils

* print multiple services

* refactoring

* feat: support 2 services at once

* add to Travis CI

* update README
  • Loading branch information
bahmutov committed Apr 14, 2020
1 parent 689f247 commit fc3efcb
Show file tree
Hide file tree
Showing 10 changed files with 830 additions and 91 deletions.
3 changes: 2 additions & 1 deletion .travis.yml
Expand Up @@ -5,7 +5,7 @@ cache:
notifications:
email: true
node_js:
- '8'
- '12'
script:
- npm test
- npm run demo
Expand All @@ -20,6 +20,7 @@ script:
- START_SERVER_AND_TEST_INSECURE=1 npm run demo9
- npm run demo-cross-env
- npm run demo-commands
- npm run demo-multiple
after_success:
- npm run travis-deploy-once "npm run semantic-release"
branches:
Expand Down
13 changes: 9 additions & 4 deletions README.md
Expand Up @@ -216,21 +216,26 @@ This utility will wait for maximum of 5 minutes while checking for the server to

### 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
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
{
"scripts": {
"test": "node src/test",
"start:api": "node src/api",
"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"
}
}
```

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

### Small print

Expand Down
157 changes: 127 additions & 30 deletions __snapshots__/utils-spec.js
@@ -1,73 +1,170 @@
exports['utils getArguments allows 5 arguments 1'] = {
"args": [
"start",
"6000",
"start:web",
"6010",
"test"
],
"parsed": {
"services": [
{
"start": "npm run start",
"url": [
"http://localhost:6000"
]
},
{
"start": "start:web",
"url": [
"http://localhost:6010"
]
}
],
"test": "npm run test"
}
}

exports['utils getArguments determines NPM script for each command 1'] = {
"args": [
"startA",
"6000",
"startB",
"6010",
"testC"
],
"parsed": {
"services": [
{
"start": "npm run startA",
"url": [
"http://localhost:6000"
]
},
{
"start": "npm run startB",
"url": [
"http://localhost:6010"
]
}
],
"test": "npm run testC"
}
}

exports['utils getArguments handles 3 arguments with http-get url 1'] = {
"start": "npm run start",
"url": [
"http-get://localhost:8080"
"services": [
{
"start": "npm run start",
"url": [
"http-get://localhost:8080"
]
}
],
"test": "npm run test"
}

exports['utils getArguments returns 3 arguments 1'] = {
"start": "npm run start",
"url": [
"http://localhost:8080"
"args": [
"start",
"8080",
"test"
],
"test": "npm run test"
"parsed": {
"services": [
{
"start": "npm run start",
"url": [
"http://localhost:8080"
]
}
],
"test": "npm run test"
}
}

exports['utils getArguments returns 3 arguments with url 1'] = {
"start": "npm run start",
"url": [
"http://localhost:8080"
"services": [
{
"start": "npm run start",
"url": [
"http://localhost:8080"
]
}
],
"test": "npm run test"
}

exports['utils getArguments understands custom commands 1'] = {
"start": "custom-command --with argument",
"url": [
"http://localhost:3000"
"services": [
{
"start": "custom-command --with argument",
"url": [
"http://localhost:3000"
]
}
],
"test": "test-command --x=1"
}

exports['utils getArguments understands several ports 1'] = {
"start": "npm run start",
"url": [
"http://localhost:3000",
"http://localhost:4000",
"http://localhost:5000"
"services": [
{
"start": "npm run start",
"url": [
"http://localhost:3000",
"http://localhost:4000",
"http://localhost:5000"
]
}
],
"test": "npm run test"
}

exports['utils getArguments understands single :port 1'] = {
"start": "npm run start",
"url": [
"http://localhost:3000"
"services": [
{
"start": "npm run start",
"url": [
"http://localhost:3000"
]
}
],
"test": "npm run test"
}

exports['utils getArguments understands single port 1'] = {
"start": "npm run start",
"url": [
"http://localhost:3000"
"services": [
{
"start": "npm run start",
"url": [
"http://localhost:3000"
]
}
],
"test": "npm run test"
}

exports['utils getArguments understands start plus url 1'] = {
"start": "start-server",
"url": [
"http://localhost:6000"
"services": [
{
"start": "start-server",
"url": [
"http://localhost:6000"
]
}
],
"test": "npm run test"
}

exports['utils getArguments understands url plus test 1'] = {
"start": "npm run start",
"url": [
"http://localhost:6000"
"services": [
{
"start": "npm run start",
"url": [
"http://localhost:6000"
]
}
],
"test": "npm run test"
}

0 comments on commit fc3efcb

Please sign in to comment.