简体   繁体   中英

How to run test file after launching server using npm scripts?

I wanted to exploit the npm script capacities and I tried to create a command to run the tests of my API. Basically I need to launch the API and perform some HTTP requests. I have the tests in a JS file, let's call it automatic-test.js , and I wanted to launch the API and then run these tests.

In my package.json , I tried this call:

"scripts": {
    "start": "node ./bin/www",
    "test": "npm start & node test/automatic-test.js"
}

The test command starts the API server but it doesn't run the tests file. I tried changing & by && , || and ; ( here ), but none of it makes the command line to reach the second part of the command. I tried to even write the content of the start command at the beginning of the test one, but the problem is the same.

I could find a workaround launching the server from the test file ( here ), require('/route/to/server/file') , leaving the test command just like node test/automatic-test.js but I feel like maybe I'm cheating with that. What should I do to make this statement work as I want to?

npm start &/&&/||/; node test/automatic-test.js

Node version: v10.15.0

Npm version: 6.4.1

EDIT: these tests are a very first draft of the future real tests, and are just a bunch of HTTP requests which should only return a very small set of results, they are not proper tests (assessments, mocha, etc.)

By default node doesn't provide ability to test directly you have to add package like mocha

Please follow the docs.

The concurrently npm package is pretty sweet, imho.

It claims to be "Like npm run watch-js & npm run watch-less but better" and since your question started with & tying the commands...

Here's an example from a package.json :

    "test:dev": "concurrently --restart-tries 10 --names rollup,sirv \"rollup --config rollup.test.config.js -w\" \"sirv test --port 3000 --single --dev\"",

It prints the names ("rollup", "sirv") to the output so it become clear, which process did what.

I'm unable to compare with start-server-and-test which was already mentioned.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM