I am trying to write a sample application for POC of mongodb-memory-server but unfortunately its not working. I checked several links and blogs but seems like code working for all is not working for me. Jest test times out even after increasing timeout to 1 min. getConnectionString or getUrl both times out. I suspect this mongodb-memory-server never starts.
Following is DB Handler for tests
I am using Windows 10
const mongoose = require('mongoose');
const { MongoMemoryServer } = require('mongodb-memory-server');
const mongoUnit = require('mongo-unit');
const mongod = new MongoMemoryServer();
jest.setTimeout(60000);
module.exports.connect = function(callback) {
mongod.getUri().then((mongoUri) => {
const mongooseOpts = {
autoReconnect: true,
reconnectTries: Number.MAX_VALUE,
reconnectInterval: 1000,
};
mongoose.connect(mongoUri, mongooseOpts);
mongoose.connection.on('error', (e) => {
if (e.message.code === 'ETIMEDOUT') {
console.log(e);
mongoose.connect(mongoUri, mongooseOpts);
}
console.log(e);
});
mongoose.connection.once('open', () => {
console.log(`MongoDB successfully connected to ${mongoUri}`);
callback();
});
});
}
module.exports.closeDatabase = async () => {
await mongoose.connection.dropDatabase();
await mongoose.connection.close();
await mongod.stop();
}
module.exports.clearDatabase = async () => {
const collections = mongoose.connection.collections;
for (const key in collections) {
const collection = collections[key];
await collection.deleteMany();
}
}