I've been utilizing Mongo Memory Server for my unit tests successfully, but all of a sudden mongoose.connection is returning as undefined. This has left me completely baffled! I would have anticipated readyState to at least be 0.
import * as mongoose from 'mongoose';
import { MongoMemoryServer } from 'mongodb-memory-server';
let con: mongoose.Connection;
let mongoServer: MongoMemoryServer;
export const connectToDatabase = async () => {
mongoServer = await MongoMemoryServer.create();
await mongoose.connect(mongoServer.getUri(), {})
.catch((err) => {
console.log(err); // no error is thrown
});
con = mongoose.connection; // Why is this turning out to be undefined?!
console.log(con.readyState); // This line seems to be triggering the error
}