测试完成后,摩卡咖啡挂起

时间:2018-06-26 15:35:15

标签: node.js mocha

我正在nodejs中运行mocha,测试一个异步函数,我没有忘记调用完成,但是在测试通过后,mocha只是挂在那里,什么也没等待。甚至我为after()放置的功能都已完成,但是直到我CTRL + C为止,mocha才退出。

代码如下:

describe("tests the user_handler", () => {
  beforeEach(resetDB);
  after(resetDB);

  it("returns null when searching for a user with an id that does not exists", (done) => {
    userHandler.findUserById( {user_id: "U-1234567"} )
      .then((result) => {
        expect(result).to.be.null;
        done()
      })
  })
})

这是输出:

tomk@tomk-Latitude-7480 ~/workspace/fundme/server (login_with_github) $ npm test

> fundme-server@1.0.0 test /home/tomk/workspace/fundme/server
> mocha tests/*.spec.js



  tests the user_handler
resetDB called
resetDB finished
    ✓ returns null when searching for a user with an id that does not exists
resetDB called
resetDB finished


  1 passing (64ms)

^CTerminated

如果有意义(尽管我不这么认为),则所测试的函数使用的是mysqlConnectionPool库中的mysql2的承诺版本

这是我用于resetDBbeforeEach的{​​{1}}函数的代码:

after

对我可能忘记的任何想法?

1 个答案:

答案 0 :(得分:4)

自从您提到您正在使用mysqlConnectionPool。我猜测您可能没有关闭池,这导致您的程序继续等待所有连接关闭。

根据文档判断:Using connection pools

  

// Don't forget to release the connection when finished!

完成后释放连接至关重要。检查并确保您{/ {1}}正在执行每个或所有测试:

after()

或者,由于这只是一个测试文件,因此关闭// For pool initialization, see above pool.getConnection(function(err, conn) { // Do something with the connection conn.query(/* ... */); // Don't forget to release the connection when finished! pool.releaseConnection(conn); }) 中的所有连接将确保mocha在末尾停止:

after
相关问题