gpt4 book ai didi

javascript - 从javascript通过端口停止进程

转载 作者:行者123 更新时间:2023-11-30 19:37:10 24 4
gpt4 key购买 nike

我正在使用 jest 为 node.js 应用程序编写一些测试。我有一个服务器 server.js 和一个测试文件 server.test.js。在我的 server.js 中,我使用了这条线

    var secureServer = https.createServer(options, 
app).listen(config.node_port, () => {
logger.info("Service running on " + config.node_port)
});

在端口 8082 上启动我的服务器。在 server.test.js 我使用

    var posClient = require('./pos-ClientJS.js');

为了访问我必须测试的功能。当我运行测试时,我得到以下控制台输出:

    Jest did not exit one second after the test run has completed.

This usually means that there are asynchronous operations that weren't
stopped in your tests. Consider running Jest with `--
detectOpenHandles` to troubleshoot this issue.

所以我的问题是:有没有一种方法可以停止使用 stop(8082) 之类的 javascript 代码运行服务器?或者有没有更简单的方法可以在不停止进程的情况下解决这个问题?

最佳答案

来自 Nodejs HTTP模块文档,可以调用secureServer.close()停止监听指定端口。如果模块将服务器暴露给 Jest,您可以使用 teardown测试完成后自动停止服务器的方法。

关于javascript - 从javascript通过端口停止进程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55815282/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com