skip to Main Content

I’m trying to get the latest version of mongodb-memory-server working with jest on a node express server. I’m trying to follow the guide in the mongodb-memory-server documentation (https://nodkz.github.io/mongodb-memory-server/docs/guides/integration-examples/test-runners#jest), but it seems to leave blanks and I can’t figure out how to fill them in.

I’ve made a repo of my best effort to piece it together: https://github.com/jimmythecode/mongodbmemoryserver-guide

I can’t find any instructions online except for older versions of mongodb-memory-server. Can anyone please help?

2

Answers


  1. Chosen as BEST ANSWER

    I've managed to get it working and have updated it in this branch, in case it helps anyone.


  2. Here is my working setup with "mongodb-memory-server": "^8.5.2" and "jest": "^28.1.0". Please Check

    import { MongoMemoryServer } from "mongodb-memory-server";
    import mongoose from "mongoose";
    
    
    let mongo: any;
    beforeAll(async () => {
      process.env.NODE_TLS_REJECT_UNAUTHORIZED = "0";
    
      mongo = await MongoMemoryServer.create();
      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 () => {
      jest.setTimeout(20000)
      await mongo.stop();
      await mongoose.connection.close();
    });
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search