简体   繁体   English

使用 npm 脚本启动服务器后如何运行测试文件?

[英]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.我想利用npm script并尝试创建一个命令来运行我的 API 测试。 Basically I need to launch the API and perform some HTTP requests.基本上我需要启动 API 并执行一些 HTTP 请求。 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.我在 JS 文件中有测试,我们称它为automatic-test.js ,我想启动 API,然后运行这些测试。

In my package.json , I tried this call:在我的package.json ,我尝试了这个调用:

"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. test命令启动 API 服务器,但它不运行测试文件。 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.我什至尝试在test一开始写start命令的内容,但问题是一样的。

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.我可以找到从测试文件( 这里)启动服务器的解决方法, require('/route/to/server/file') ,让test命令就像node test/automatic-test.js但我觉得也许我我在作弊。 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节点版本:v10.15.0

Npm version: 6.4.1 NPM 版本: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.)编辑:这些测试是未来真实测试的初稿,只是一堆 HTTP 请求,应该只返回一小部分结果,它们不是正确的测试(评估、摩卡等)

By default node doesn't provide ability to test directly you have to add package like mocha默认情况下,节点不提供直接测试的能力,您必须添加像mocha这样的包

Please follow the docs.请遵循文档。

The concurrently npm package is pretty sweet, imho. concurrently npm 包非常可爱,恕我直言。

It claims to be "Like npm run watch-js & npm run watch-less but better" and since your question started with & tying the commands...它声称是“像 npm run watch-js & npm run watch-less 但更好”,并且由于您的问题始于&绑定命令......

Here's an example from a package.json :这是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.它将名称(“rollup”、“sirv”)打印到输出中,以便清楚知道哪个进程做了什么。

I'm unable to compare with start-server-and-test which was already mentioned.我无法与已经提到的start-server-and-test进行比较。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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