如何在玩笑测试中正确关闭猫鼬连接

时间:2018-06-26 18:08:27

标签: mongodb testing mongoose jestjs

使用--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() 

但是我仍然看到此错误

该如何解决?

0 个答案:

没有答案