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?
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.
npm start is the short form for npm run start . So, its one and the same thing.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With