我正在使用MongoDB,NodeJs和Chai来测试代码。
连接到mongodb:
if (!!connection) {
model = connection.model('client', clientSchema, collections.clients[env]);
} else {
model = mongoose.model('client', clientSchema, collections.clients[env]);
}
这是单元测试:
describe('Bad VSA card test', () => {
it('should contains VSA accounts', async (done) => {
const vsaCard = await updatedVSA();
console.log(vsaCard);
expect(vsaCard.length).to.equal(5);
done();
});
afterAll(() => {
client.disconnect();
});
});
运行此测试代码时,出现错误:
Jest did not exit one second after the test run has completed.
This usually means that there are asynchronous operations that weren't stopped in your tests. Consider running Jest with `--detectOpenHandles` to troubleshoot this issue.
实际上,我首先搜索了此错误。我发现了类似的问题。通过关闭数据库连接修复了他的错误。为什么关闭MongoDB连接仍会出现此错误?