mardi 3 octobre 2017

Is it possible to start and stop the NodeJS webserver by Mocha's framework?

I'm trying to do this tutorial with one little change. http://ift.tt/1chOsCT

I want to automatically start my webserver when the Mocha framework starts, when npm test command is executed. I don't want to manually start the webserver, run the Mocha tests and then stop the webserver.

If I use the test command according to the tutorial, it will not start the webserver automatically:

  "scripts": {
    "test": "./node_modules/.bin/mocha --reporter spec"
  }

This command seems to me stucked at the first stage, the tests don't run.

  "scripts": {
    "test": "node app/server && ./node_modules/.bin/mocha --reporter spec && ... && TERMINATE_SERVER && ..."
  }

I've tried to run a js file, but in this case other problems will appear (like creating OS specific scripts)

  "scripts": {
    "test": "node ./test/testinit.js"
  }

e.g.: testinit.js:

if (process.platform == "linux"){
    script = "testInitializer.sh"
} else { //(process.platform == "win32")
    script = "testInitializer.bat"
}

after that I have to use some shell specific commands in synchronous way in my scripts:

const execSync = require('child_process').execSync;
execSync("..."); // start
// run the tests
execSync("..."); // stop

... that seems to me too complicated.

What can I do if I don't want to create multiple OS dependent solutions for this problem? Is it possible to start and stop the webserver by the framework only when it's needed?

Thank you for your help!

Aucun commentaire:

Enregistrer un commentaire