与 Mongoose 连接时,Mocha 在执行后挂起
Mocha hangs after execution when connecting with Mongoose
Talk is cheap, show me the code
Linus Torvald
正在与 mocha
和 supertest
进行集成测试。这是代码
//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);
});
基本上发生的事情是测试后的输出 "Connected to mongodb" 日志是 运行(我只有 3 个测试,none 使用数据库),然后 mocha 挂在那里我必须 Ctrl+C。我可能错过了一些配置,但我看不到它。
不用说,评论猫鼬行 (mongoose.connect(...)
) 修复它。
我错过了什么?
测试完成后,您必须断开与数据库的连接。例如,这可以在 after
函数中完成。
after((done) => {
app.close(() => {
mongoose.connection.close(done);
});
});
如果您不断开连接,您将出现您所描述的症状。
更简单的答案
after((done) => {
mongoose.connection.close(done);
});
Talk is cheap, show me the code
Linus Torvald
正在与 mocha
和 supertest
进行集成测试。这是代码
//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);
});
基本上发生的事情是测试后的输出 "Connected to mongodb" 日志是 运行(我只有 3 个测试,none 使用数据库),然后 mocha 挂在那里我必须 Ctrl+C。我可能错过了一些配置,但我看不到它。
不用说,评论猫鼬行 (mongoose.connect(...)
) 修复它。
我错过了什么?
测试完成后,您必须断开与数据库的连接。例如,这可以在 after
函数中完成。
after((done) => {
app.close(() => {
mongoose.connection.close(done);
});
});
如果您不断开连接,您将出现您所描述的症状。
更简单的答案
after((done) => {
mongoose.connection.close(done);
});