简体   繁体   English

如何彻底退出CI的npm服务器测试?

[英]How can I cleanly exit an npm server test for CI?

I've been developing a node based server that I want to have tested on Travis-ci, the problem is the only thing I want to test now is if the server has any errors on start 我一直在开发要在Travis-ci上测试过的基于节点的服务器,问题是我现在要测试的唯一一件事是服务器启动时是否有任何错误

(It's just a backend for serving an API; all it does on start is serve out of a port). (它只是提供API的后端;启动时所做的只是从端口提供服务)。

If it starts fine, I don't want it to run forever on the CI's VM. 如果启动正常,我不希望它在CI的VM上永远运行。 I've tried having the test script in my package.json point to a bash script that uses kill with the PID but that doesn't exit cleanly to pass Travis's build. 我尝试将test脚本包含在package.json中,指向一个bash脚本,该脚本使用PID的kill ,但不会干净退出以通过Travis的构建。

Long story short, what should I write to have my test run npm start for a couple of seconds and then close node cleanly if there are no errors? 长话短说,我应该写些什么来让我的测试运行npm start几秒钟,然后在没有错误的情况下彻底关闭节点?

You could add a wrapper around your server that you run as your test in which you can exit after x amount of seconds or if an error occurs. 您可以在作为测试运行的服务器周围添加包装器,在x秒后或发生错误时可以退出包装器。 Something like this: 像这样:

require('./myserver') // i asume this starts your server
setTimeout(() => process.exit(0), 5000) // exits with 0 exit code after 5 seconds
process.on('uncaughtException', (err) => {
  console.log(err)
  process.exit(1)
})

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

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