操作状态不正确:"new",允许状态:"[running,starting]"



我只测试了一个测试用例。我验证了MongoDB方法,它们似乎达到了日期GitHub上也没有公开的问题。

Error:    at MongoMemoryServer.getUri (node_modules/mongodb-memory-server- 
core/src/MongoMemoryServer.ts:706:15)
at src/test/setup.ts:7:32
at src/test/setup.ts:8:71
at Object.<anonymous>.__awaiter (src/test/setup.ts:4:12)
at Object.<anonymous> (src/test/setup.ts:5:22)   

import { MongoMemoryServer } from "mongodb-memory-server";
import mongoose from "mongoose";
let mongo: any;
beforeAll(async () => {
mongo = new MongoMemoryServer();
const mongoUri = await mongo.getUri();
await mongoose.connect(mongoUri);
});
beforeEach(async () => {
const collections = await mongoose.connection.db.collections();
for (let collection of collections) {
await collection.deleteMany({});
}
});
afterAll(async () => {
await mongo.stop();
await mongoose.connection.close();
});

将线路mongo = new MongoMemoryServer();切换到mongo = await MongoMemoryServer.create()

我遇到了同样的问题,通过查看文档,我发现您首先必须使用new创建对象,然后使用start()启动实例:

mongod = new MongoMemoryServer()
await mongod.start()
const uri = await mongod.getUri()
const mongooseOptions = {
useNewUrlParser:true,
};
await mongoose.connect(uri,mongooseOptions)

您有两个选项来解决此问题:

1-更改线路:

mongo = new MongoMemoryServer(); 

至:

mongo = await MongoMemoryServer.create();

2-更改线路:

mongo = new MongoMemoryServer();
const mongoUri = await mongo.getUri();
await mongoose.connect(mongoUri);

收件人:

mongod = new MongoMemoryServer()
await mongod.start()
const uri = await mongod.getUri()
const mongooseOptions = {
useNewUrlParser:true,
};
await mongoose.connect(uri, mongooseOptions)

相关内容

  • 没有找到相关文章

最新更新