node.js - 在 AWS Codebuild 上运行时 Jest 不关闭()ing expressjs 服务器

标签 node.js express testing jestjs aws-codebuild

当我在本地运行时,jest 退出正常,但是当在 codebuild 上运行时,jest 不会退出并给出此错误:

Jest has detected the following 1 open handle potentially keeping Jest from exiting:

● TCPSERVERWRAP

  13 | routes(app) 
  14 |  
> 15 | app.listen(port, async err => { 

这两种变体都可以在本地工作,但不能在代码构建上工作:

  afterAll((done) => {
    if (app) {
      app.close(done);
    }
  })
  afterAll(async () => {
    if (app) {
      await app.close()      
    }
  })

使用 process.exit(0) 没有帮助

最佳答案

好吧,有点业余错误。当我包装 app.close() 调用时,结果发现它是未定义的。我试图接近 express request 对象。我必须从实例化 express 的地方导出服务器对象:

  afterAll(async () => {  
    try {
      await server.close()      
    } catch (error) {
      console.error(error)
      throw error;
    }
  })
const app = express()
routes(app)

const server = app.listen(port, async err => {
  ...

module.exports = app
module.exports.server = server

关于node.js - 在 AWS Codebuild 上运行时 Jest 不关闭()ing expressjs 服务器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54599130/

相关文章:

javascript - 如何将数据发送到jade(pug)中的JSON属性?

javascript - 如何使用 PG 在 Node JS 中获取 client.query 的值?

testing - Lettuce 和 Pyccuracy 之间有什么区别?

testing - Cypress 不匹配路线

perl - 是否有 PDF 文件的测试套件?

ruby-on-rails - 将 Angular 2 应用程序部署到 Heroku

node.js - npm 包的可安装 typescript 类型定义

node.js - nodejs child_process 子进程运行两次

javascript - Class.method 不是使用 ES6 类的函数

node.js - 如何像其他流行的 mvc 框架一样在环回中包含服务层?