'mongo-inmemory-server in Nestjs throws MongoNotConnectedError
consider this test file
describe('UserRepository', () => {
let userRepository: UserRepository;
let module: TestingModule;
beforeAll(async () => {
module = await Test.createTestingModule({
imports: [
rootMongooseTestModule(),
MongooseModule.forFeature([{ name: 'User', schema: UserSchema }]),
],
providers: [UserRepository],
}).compile();
userRepository = module.get<UserRepository>(UserRepository);
});
it('should be defined', () => {
expect(userRepository).toBeDefined();
});
it('can be created correctly', async () => {
expect(
async () =>
await userRepository.create({
firstName: 'Merlin',
phone: '734214353',
email: '[email protected]',
}),
).not.toThrow();
});
and also this is the in memory config :
export const rootMongooseTestModule = (options: MongooseModuleOptions = {}) =>
MongooseModule.forRootAsync({
useFactory: async () => {
mongod = await MongoMemoryServer.create();
const mongoUri = mongod.getUri();
console.log(mongoUri); //==========> logs mongodb://127.0.0.1:37345/
return {
uri: mongoUri,
...options,
};
},
});
export const closeInMongodConnection = async () => {
await mongoose.disconnect();
if (mongod) await mongod.stop();
};
the `should be defined` test is passed correctly,
but the second test throws this `Test suite failed to run`
`MongoNotConnectedError: MongoClient must be connected to perform this operation`
so as commented in code, the mongo uri is genereted correctly, how to handle the connection?
Solution 1:[1]
The correct answer to this question is "This program is ill-formed". I don't think this is the answer the professor is expecting.
The C++ standards do not specify any behaviour for ill-formed programs. In a very real sense, it is not a C++ program.
It also looks like Microsoft don't define what Visual Studio does with that code, only that it does something, and that something isn't compliant with ISO C++.
If you instead had const Hello& hob = Aloha("Bob");, there wouldn't be a spurious "Aloha Bye, Bob" in the output, because the Aloha wouldn't be destroyed on that line.
Similarly Hello bob = Aloha("Bob"); is correctly rejected by Visual Studio.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Caleth |
