mongodb-memory-server: "MongoMemoryReplSet" in "jest" gives open handle error

If I close the client & await replSet.stop(), jest spits out an error:

Jest did not exit one second after the test run has completed.

This usually means that there are asynchronous operations that weren't stopped in your tests. Consider running Jest with `--detectOpenHandles` to troubleshoot this issue.

If I don’t use a replset, then it works.

describe('MyTests', () => {
  let replSet: MongoMemoryReplSet;
  let client: MongoClient;

  beforeAll(async () => {
    replSet = new MongoMemoryReplSet({
      binary: {
        version: '4.0.5'
      },
      instanceOpts: [{
        storageEngine: 'wiredTiger'
      }],
      autoStart: true,
      debug: true
    });

    await replSet.waitUntilRunning();

    // ... connect to client, etc
  });

  afterAll(async () => {
    await client.close();
    await replSet.stop();
  });

  // ...
});

If I replace the above with just a single mongo server and stop it, it works.

About this issue

  • Original URL
  • State: closed
  • Created 5 years ago
  • Reactions: 3
  • Comments: 20

Most upvoted comments

@abishek28007 like i said, there isnt an “smooth” transition release yet, please read the changelog on what changed

not sure what was added in beta 7 but getting “instanceInfo” is undefined for const uri = await mongod.getUri(); for MongoMemoryServer

this is because getUri is in 7.0.0 not async anymore and dosnt wait for the server start anymore


aside from this, autoStart got removed, so you need to either manually call .start, or use the static method .create