skip to Main Content

There is just one test case that I’m testing. I verified the MongoDB methods and they seem to be up to
date. No open issues on GitHub as well.

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();
});

3

Answers


  1. channge the line mongo = new MongoMemoryServer(); to mongo = await MongoMemoryServer.create()

    Login or Signup to reply.
  2. I had your same issue and by looking at the documentation I figure out that you first have to create the object with new and then start the instance with start():

    mongod = new MongoMemoryServer()
    await mongod.start()
    
    const uri = await mongod.getUri()
    
    const mongooseOptions = {
        useNewUrlParser:true,
    };
    
    await mongoose.connect(uri,mongooseOptions)
    
    Login or Signup to reply.
  3. You have 2 options to solve this problem:

    1- Change the line:

    mongo = new MongoMemoryServer(); 
    

    to:

    mongo = await MongoMemoryServer.create();
    

    2- Change the lines:

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

    To:

    mongod = new MongoMemoryServer()
    await mongod.start()
    
    const uri = await mongod.getUri()
    
    const mongooseOptions = {
    useNewUrlParser:true,
    };
    
    await mongoose.connect(uri, mongooseOptions)
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search