与Mongoose连接时,Mocha在执行后挂起 [英] Mocha hangs after execution when connecting with Mongoose
问题描述
对话很便宜,请告诉我代码
Linus Torvald
Linus Torvald
使用mocha
和supertest
进行集成测试.这是代码
Doing integration tests with mocha
and supertest
. Here's the code
//app.js
mongoose.Promise = global.Promise;
mongoose.connect(config.mongoURL, error => {
if (error) {
throw error;
}
console.log('Connected to mongodb');
});
modules.export = app;
// test.js
it('returns 200', () => {
return supertest(app).get('/').expect(200);
});
基本上发生的是在测试运行后输出已连接到mongodb"日志(我只喜欢3个测试,没有一个使用db),然后mocha挂在那里,我必须 Ctrl + C .我可能错过了一些配置,但看不到.
Basically what happens is that the output "Connected to mongodb" logs after the tests are run (I have like 3 tests only, none use the db), and afterwards mocha hangs there and I have to Ctrl+C. I probably missed some configuration but I can't see it.
不用说,注释猫鼬行(mongoose.connect(...)
)即可解决此问题.
Needless to say, commenting the mongoose lines (mongoose.connect(...)
) fixes it.
我想念什么?
推荐答案
测试完成后,您必须与数据库断开连接.例如,可以在after
函数中完成此操作.
You have to disconnect from the database after the tests are done. This can be done in the after
function, for example.
after((done) => {
app.close(() => {
mongoose.connection.close(done);
});
});
如果不断开连接,您将得到正在描述的症状.
If you don't disconnect you'll get the symptoms you are describing.
这篇关于与Mongoose连接时,Mocha在执行后挂起的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!