mocha 在测试完成后挂起
mocha hangs after tests have finished
我是nodejs中的运行mocha,测试一个异步函数,我没有忘记调用done,但是测试通过后mocha就挂在那里,什么都不等。即使我为 after()
放置的功能已经完成,但 mocha 直到我 CTRL+C 才退出。
代码如下:
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
如果它是相关的(虽然我不这么认为),被测试的函数正在使用 mysql2
库
中的 mysqlConnectionPool
的 promisified 版本
这是我用于 beforeEach
和 after
的 resetDB
函数的代码:
function resetDB() {
console.log("resetDB called")
command =
"mysql" +
" --defaults-extra-file=" + mysql_conf_file +
" --host " + process.env['MYSQL_HOSTNAME'] +
" -D fundme < " + testing_db_file_location;
cp.execSync(command);
console.log("resetDB finished")
}
有什么我可能忘记的想法吗?
既然你提到你正在使用 mysqlConnectionPool
。我猜你可能没有关闭导致你的程序继续等待所有连接关闭的池。
根据文档判断:Using connection pools
// Don't forget to release the connection when finished!
完成后释放连接很重要。检查并确保您正在执行此操作 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);
})
或者,因为这只是一个测试文件,关闭 after
中的所有连接将确保 mocha 在最后停止:
after(() => { mysqlConnectionPool.end() })
我是nodejs中的运行mocha,测试一个异步函数,我没有忘记调用done,但是测试通过后mocha就挂在那里,什么都不等。即使我为 after()
放置的功能已经完成,但 mocha 直到我 CTRL+C 才退出。
代码如下:
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
如果它是相关的(虽然我不这么认为),被测试的函数正在使用 mysql2
库
mysqlConnectionPool
的 promisified 版本
这是我用于 beforeEach
和 after
的 resetDB
函数的代码:
function resetDB() {
console.log("resetDB called")
command =
"mysql" +
" --defaults-extra-file=" + mysql_conf_file +
" --host " + process.env['MYSQL_HOSTNAME'] +
" -D fundme < " + testing_db_file_location;
cp.execSync(command);
console.log("resetDB finished")
}
有什么我可能忘记的想法吗?
既然你提到你正在使用 mysqlConnectionPool
。我猜你可能没有关闭导致你的程序继续等待所有连接关闭的池。
根据文档判断:Using connection pools
// Don't forget to release the connection when finished!
完成后释放连接很重要。检查并确保您正在执行此操作 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);
})
或者,因为这只是一个测试文件,关闭 after
中的所有连接将确保 mocha 在最后停止:
after(() => { mysqlConnectionPool.end() })