title: MongoError: pool is draining, new operations prohibited when using MongoMemoryServer in integration test
body: <p>I'm using <code>MongoMemoryServer</code> to write an integration test. I have two integration test files. When I run the IT tests I see the following. I don't understand why. I'm using jestjs test framework.</p>
  
  <p>I'm seeing the following error when I have two IT test files</p>
  
  <pre><code>MongoError: pool is draining, new operations prohibited
  
        37 |   for (const key in collections) {
        38 |     const collection = collections[key];
      &gt; 39 |     await collection.deleteMany();
           |                      ^
        40 |   }
        41 | };
  </code></pre>
  
  <p>Here is my setup</p>
  
  <pre><code>//db-handler.js
  const mongoose = require("mongoose");
  const { MongoMemoryServer } = require("mongodb-memory-server");
  const mongod = new MongoMemoryServer();
  
  module.exports.connect = async () =&gt; {
    const uri = await mongod.getConnectionString();
  
    const mongooseOpts = {
      useNewUrlParser: true,
      autoReconnect: true,
      reconnectTries: Number.MAX_VALUE,
      reconnectInterval: 1000,
    };
  
    await mongoose.connect(uri, mongooseOpts);
  };
  
  module.exports.closeDatabase = async () =&gt; {
    await mongoose.connection.dropDatabase();
    await mongoose.connection.close();
    await mongod.stop();
  };
  
  module.exports.clearDatabase = async () =&gt; {
    const collections = mongoose.connection.collections;
  
    for (const key in collections) {
      const collection = collections[key];
      await collection.deleteMany();
    }
  };
  </code></pre>
  
  <p>All my IT tests setup looks like this</p>
  
  <pre><code>//example.it.test.js
  const supertest = require("supertest");
  const dbHandler = require("./db-handler");
  const app = require("../../src/app");
  const request = supertest(app);
  
  const SomeModel = require("../Some");
  beforeAll(async () =&gt; await dbHandler.connect());
  afterEach(async () =&gt; await dbHandler.clearDatabase());
  afterAll(async () =&gt; await dbHandler.closeDatabase());
  
  describe("Some Test Block", () =&gt; {
    it("Test One", async (done) =&gt; {
      await SomeModel.create({a: "a", b "b"});
  
      const response = await request.get("/endPointToTest");
      expect(response.status).toBe(200);
  
      done();
    });
  </code></pre>
  
  <p>When I have just a single IT test files, everything works fine. When I introduce a a new IT test file similar setup up as <code>example.it.test.js</code>, then the new test fails. The example error message above. </p>
  
  <p>What am I missing? Does my setup need to change when I have multiple IT test files?</p>
  
  <p>UPDATE ONE:
  My package.json file looks like</p>
  
  <pre><code>{
    "scripts": {
      "start": "node index.js",
      "dev": "nodemon index.js",
      "test": "jest --runInBand ./tests"
    },
    "devDependencies": {
      "jest": "^25.2.7",
      "mongodb-memory-server": "^6.5.2",
      "nodemon": "^2.0.2",
      "supertest": "^4.0.2"
    },
    "jest": {
      "testEnvironment": "node",
      "coveragePathIgnorePatterns": [
        "/node_modules/"
      ]
    }
  }
  </code></pre>