【问题标题】:Jest not close()ing expressjs server when run on AWS Codebuild在 AWS Codebuild 上运行时开玩笑不关闭()expressjs 服务器
【发布时间】:2019-02-09 19:07:08
【问题描述】:

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

Jest 检测到以下 1 个可能阻止 Jest 退出的打开句柄:

● TCPSERVERWRAP

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

这两种变体都可以在本地工作,但不能在 codebuild 上工作:

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

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

【问题讨论】:

    标签: node.js express testing jestjs aws-codebuild


    【解决方案1】:

    好的,有点业余错误。当我包装app.close() 调用时,结果发现它是未定义的。我试图接近快递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
    

    【讨论】:

      猜你喜欢
      • 2021-11-13
      • 2020-10-08
      • 2021-03-23
      • 1970-01-01
      • 2019-01-08
      • 2019-11-06
      • 1970-01-01
      • 1970-01-01
      • 2021-12-13
      相关资源
      最近更新 更多