如何在 CI 环境中运行 postman 的 newman?

2022-01-22 00:00:00 node.js postman javascript npm newman

我想在我的 CI 环境 (solano-ci) 上运行 newman.

I'd like to run newman on my CI environment (solano-ci).

newman 是一个在邮递员集合中运行请求的工具.

newman is a tool that runs through requests in your postman collection.

我的 package.json 中有一个 newman 脚本,我还有一个 npm start 脚本,它在 处启动服务器>localhost:3000.

I have a newman script in my package.json, and I also have a npm start script that starts the server at localhost:3000.

newman 已经配置了环境变量来测试端口 localhost:3000 上的端点.

newman is already configured with environment variables to test endpoints on port localhost:3000.

问题是我需要一个脚本来启动服务器 (npm start),然后运行 ​​npm run newman.但是在服务器可用和 newman 运行它的测试之间存在延迟.如果 newman 在服务器可用之前运行,则每次测试都会出错.

The issue is I need one script that starts the server (npm start) and then runs npm run newman. But there's a delay between when the server is available and when newman runs it's tests. If newman runs before the server is available it results in an error for each test.

Error: connect ECONNREFUSED 127.0.0.1:3000

现在这是我尝试使用 run-p 用于并行启动两个进程.然后我必须使用 sleep 并设置任意数量的等待时间以确保服务器准备就绪.

Right now here's what I tried using run-p which works at starting two processes in parallel. Then I have to use sleep and set an arbitrary number of time the wait to ensure that the server is ready.

"newman": "newman -c ./postman/api.postman_collection.json -e ./postman/local.postman_environment.json",
"newman-sleep": "sleep 10 && npm run newman",
"newman-server": "run-p start newman-sleep"

推荐答案

你可以使用 wait-on 包.

npm install --save-dev wait-on

那么,

"newman-sleep": "wait-on http://localhost:3000 && npm run newman"

相关文章