UNPKG

8.76 kBMarkdownView Raw
1# start-server-and-test
2
3> Starts server, waits for URL, then runs test command; when the tests end, shuts down server
4
5[![NPM][npm-icon] ][npm-url]
6
7[![Build status][ci-image] ][ci-url]
8[![semantic-release][semantic-image] ][semantic-url]
9[![js-standard-style][standard-image]][standard-url]
10[![renovate-app badge][renovate-badge]][renovate-app]
11
12## Install
13
14Requires [Node](https://nodejs.org/en/) version 8.9 or above.
15
16```sh
17npm install --save-dev start-server-and-test
18```
19
20## Use
21
22This command is meant to be used with NPM script commands. If you have a "start server", and "test" script names for example, you can start the server, wait for a url to respond, then run tests. When the test process exits, the server is shut down.
23
24```json
25{
26 "scripts": {
27 "start-server": "npm start",
28 "test": "mocha e2e-spec.js",
29 "ci": "start-server-and-test start-server http://localhost:8080 test"
30 }
31}
32```
33
34To execute all tests simply run `npm run ci`
35
36### Commands
37
38In addition to using NPM script names, you can pass entire commands (surround them with quotes so it is still a single string) that will be executed "as is". For example, to start globally installed `http-server` before running and recording [Cypress.io](https://www.cypress.io) tests you can use
39
40```
41start-server-and-test 'http-server -c-1 --silent' 8000 './node_modules/.bin/cypress run --record'
42```
43
44or because `npm` scripts execute with `./node_modules/.bin` in the `$PATH`, you can mix global and locally installed tools:
45
46```json
47{
48 "scripts": {
49 "ci": "start-server-and-test 'http-server -c-1 --silent' 8080 'mocha e2e-spec.js'"
50 }
51}
52```
53
54### Alias
55
56You can use either `start-server-and-test`, `server-test` or `start-test` commands in your scripts.
57
58You can use `:` in front of port number like `server-test :8080`, so all these are equivalent
59
60```
61start-server-and-test start http://localhost:8080 test
62server-test start http://localhost:8080 test
63server-test http://localhost:8080 test
64start-test :8080 test
65start-test 8080 test
66start-test 8080
67```
68
69### Options
70
71If you use convention and name your scripts "start" and "test" you can simply provide URL
72
73```json
74{
75 "scripts": {
76 "start": "npm start",
77 "test": "mocha e2e-spec.js",
78 "ci": "start-server-and-test http://localhost:8080"
79 }
80}
81```
82
83You can also shorten local url to just port, the code below is equivalent to checking `http://localhost:8080`.
84
85```json
86{
87 "scripts": {
88 "start": "npm start",
89 "test": "mocha e2e-spec.js",
90 "ci": "server-test 8080"
91 }
92}
93```
94
95You can provide first start command, port (or url) and implicit `test` command
96
97```json
98{
99 "scripts": {
100 "start-it": "npm start",
101 "test": "mocha e2e-spec.js",
102 "ci": "server-test start-it 8080"
103 }
104}
105```
106
107You can provide port number and custom test command, in that case `npm start` is assumed to start the server.
108
109```json
110{
111 "scripts": {
112 "start": "npm start",
113 "test-it": "mocha e2e-spec.js",
114 "ci": "server-test :9000 test-it"
115 }
116}
117```
118
119You can provide multiple resources to wait on, separated by a pipe `|`. _(be sure to wrap in quotes)_
120
121```json
122{
123 "scripts": {
124 "start": "npm start",
125 "test-it": "mocha e2e-spec.js",
126 "ci": "server-test \"8080|http://foo.com\""
127 }
128}
129```
130
131or for multiple ports simply: `server-test '8000|9000' test`.
132
133## `npx` and `yarn`
134
135If you have [npx](https://www.npmjs.com/package/npx) available, you can execute locally installed tools from the shell. For example, if the `package.json` has the following local tools:
136
137```json
138{
139 "devDependencies": {
140 "cypress": "3.2.0",
141 "http-server": "0.11.1",
142 "start-server-and-test": "1.9.0"
143 }
144}
145```
146
147Then you can execute tests simply:
148
149```text
150$ npx start-test 'http-server -c-1 .' 8080 'cypress run'
151starting server using command "http-server -c-1 ."
152and when url "http://localhost:8080" is responding
153running tests using command "cypress run"
154Starting up http-server, serving .
155...
156```
157
158Similarly, you can use [yarn](https://yarnpkg.com/en/) to call locally installed tools
159
160```text
161$ yarn start-test 'http-server -c-1 .' 8080 'cypress run'
162yarn run v1.13.0
163$ /private/tmp/test-t/node_modules/.bin/start-test 'http-server -c-1 .' 8080 'cypress run'
164starting server using command "http-server -c-1 ."
165and when url "http://localhost:8080" is responding
166running tests using command "cypress run"
167Starting up http-server, serving .
168...
169```
170
171## Note for webpack-dev-server users
172
173If you are using [webpack-dev-server](https://www.npmjs.com/package/webpack-dev-server) (directly or via `angular/cli` or other boilerplates) then please use the following URL form to check
174
175```
176start-server-and-test http-get://localhost:8080
177```
178
179This is because under the hood this module uses [wait-on](https://github.com/jeffbski/wait-on) to ping the server. Wait-on uses `HEAD` by default, but `webpack-dev-server` does not respond to `HEAD` only to `GET` requests. Thus you need to use `http-get://` URL format to force `wait-on` to use `GET` probe.
180
181You can even wait on the bundle JavaScript url instead of the page url, see discussion in this [issue #4](https://github.com/bahmutov/start-server-and-test/issues/4)
182
183### Debugging
184
185To see diagnostic messages, run with environment variable `DEBUG=start-server-and-test`
186
187### Disable HTTPS certificate checks
188
189To see disable HTTPS checks for `wait-on`, run with environment variable `START_SERVER_AND_TEST_INSECURE=1`.
190
191### Timeout
192
193This utility will wait for maximum of 5 minutes while checking for the server to respond (default). Setting an environment variable `WAIT_ON_TIMEOUT=600000` (milliseconds) sets the timeout for example to 10 minutes.
194
195### Starting two servers
196
197Sometimes 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
198
199```json
200{
201 "scripts": {
202 "test": "node src/test",
203 "start:api": "node src/api",
204 "start:server": "node src/server",
205 "start:server-and-api": "start-test start:api 7600 start:server",
206 "test:all": "start-test start:server-and-api 5000 test"
207 }
208}
209```
210
211In 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
212
213### Small print
214
215Author: Gleb Bahmutov <gleb.bahmutov@gmail.com> © 2017
216
217* [@bahmutov](https://twitter.com/bahmutov)
218* [glebbahmutov.com](https://glebbahmutov.com)
219* [blog](https://glebbahmutov.com/blog)
220
221License: MIT - do anything with the code, but don't blame me if it does not work.
222
223Support: if you find any problems with this module, email / tweet /
224[open issue](https://github.com/bahmutov/start-server-and-test/issues) on Github
225
226## MIT License
227
228Copyright (c) 2017 Gleb Bahmutov <gleb.bahmutov@gmail.com>
229
230Permission is hereby granted, free of charge, to any person
231obtaining a copy of this software and associated documentation
232files (the "Software"), to deal in the Software without
233restriction, including without limitation the rights to use,
234copy, modify, merge, publish, distribute, sublicense, and/or sell
235copies of the Software, and to permit persons to whom the
236Software is furnished to do so, subject to the following
237conditions:
238
239The above copyright notice and this permission notice shall be
240included in all copies or substantial portions of the Software.
241
242THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
243EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
244OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
245NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
246HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
247WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
248FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
249OTHER DEALINGS IN THE SOFTWARE.
250
251[npm-icon]: https://nodei.co/npm/start-server-and-test.svg?downloads=true
252[npm-url]: https://npmjs.org/package/start-server-and-test
253[ci-image]: https://travis-ci.org/bahmutov/start-server-and-test.svg?branch=master
254[ci-url]: https://travis-ci.org/bahmutov/start-server-and-test
255[semantic-image]: https://img.shields.io/badge/%20%20%F0%9F%93%A6%F0%9F%9A%80-semantic--release-e10079.svg
256[semantic-url]: https://github.com/semantic-release/semantic-release
257[standard-image]: https://img.shields.io/badge/code%20style-standard-brightgreen.svg
258[standard-url]: http://standardjs.com/
259[renovate-badge]: https://img.shields.io/badge/renovate-app-blue.svg
260[renovate-app]: https://renovateapp.com/