Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to wait for server start complete from an npm script?

I'm trying to run a functional test for a node app.

In my package.json I have the following scripts:

"scripts": {
  "web-server": "NODE_ENV=test node app.js &",
  "test": "npm run web-server && mocha ./tests/functional/*.js --exit",
  "posttest": "pkill -f node"
}

But when running it, tests run before the server completes starting.

How can I wait for the server?

like image 540
user3552178 Avatar asked Sep 30 '19 18:09

user3552178


People also ask

What is npm wait?

wait-on will wait for period of time for a file to stop growing before triggering availability which is good for monitoring files that are being built. Likewise wait-on will wait for period of time for other resources to remain available before triggering success.

What is the difference between npm start and npm run start?

npm start is the short form for npm run start . So, its one and the same thing.

How does npm run start work?

So npm start runs the node script that is listed under start in the package. json. As in the article that cbr mentioned in a comment, in the case of create-react-app , this happens: A new instance of the WebpackDevServer from the library of the same name is created, passing in the compiler and configuration.


1 Answers

I found wait-on today and like its approach. It only does the wait, not other things like command launching.

Using it with concurrently, like so:

"scripts": {
    "xxx": "concurrently -n server,mocha \"npm run web-server\" \"npx wait-on http://localhost:8080 && npx mocha ...\"",

Wanted to mention to new visitors. I think wait-on is currently the best fitting answer to the title's question.

like image 95
akauppi Avatar answered Oct 03 '22 20:10

akauppi