Node.js 来自mongodb内存服务器且带有jest的MongoMemoryReplSet抛出打开句柄错误
运行测试文件会引发以下错误:Node.js 来自mongodb内存服务器且带有jest的MongoMemoryReplSet抛出打开句柄错误,node.js,mongodb,jestjs,Node.js,Mongodb,Jestjs,运行测试文件会引发以下错误: 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.
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内存服务器的replset
const mongoose = require('mongoose');
const { MongoMemoryReplSet } = require('mongodb-memory-server');
const replSet = new MongoMemoryReplSet({
replSet: { storageEngine: 'wiredTiger' },
});
初始化数据库:
beforeAll(async () => {
await replSet.waitUntilRunning();
const uri = await replSet.getUri();
await mongoose.connect(uri,
{
useNewUrlParser: true,
useUnifiedTopology: true,
useCreateIndex: true,
useFindAndModify: false
}, (err) => {
if (err) {
console.error(err);
process.exit(1);
}
});
await new User(testUser).save();
});
拆卸:
afterAll(async()=>{
await mongoose.connection.close();
await replSet.stop()
})
afterAll(async()=>{
await mongoose.connection.close();
await replSet.stop()
})