mongodb - 如何在开玩笑测试中正确关闭猫鼬连接
问题描述
使用 --detectOpenHandles 参数运行测试后出现以下错误
Jest has detected the following 1 open handle potentially keeping Jest from exiting:
● PROMISE
18 |
19 | mongoose.Promise = global.Promise;
> 20 | mongoose.connect(config.database.link, config.database.options);
| ^
21 |
22 |
23 | app.use(cors());
但我的测试包括 mongoose.deisconnect()
afterAll(() => {
return new Promise(res => mongoose.disconnect(() => {
res();
}));
});
我试图将 afteAll 函数更改为这样的东西
afterAll(async () => {
await mongoose.disconnect();
await mongoose.connection.close();
});
我也尝试在 afterAll() 内部调用 con.disconnect
app.con = mongoose.connect(config.database.link, config.database.options);
// inside of afterAll
app.con.disconnect()
但我仍然看到这个错误
如何解决?
解决方案
推荐阅读
- vue.js - 使用来自 vue 客户端的 mongoose 进行模糊搜索
- vue.js - 在进入路由之前刷新令牌Vuex
- javascript - React Hooks(useState)中的推送方法?
- python - 在列表python中查找下一个实体
- twilio - 响铃组:将后续呼叫与初始呼叫关联
- javascript - 函数 javascript args 像 jQuery 一样接收值
- swift - 计算字符串几何尺寸时的差异
- javascript - 当我单击单选按钮时,它的 true 或自动其他 Span 值变为 false
- haskell - 对列表中的一对列表 Haskell
- reactjs - 使用 HOC、React Context 和 componentDidMount API 从 Firebase 获取 uid