title: Best way to setup files for jest and mongodb
body: <p>Hihi.
  I'm having a problem where jest it's sometimes failing a couple of tests randomly, most of the time because of this error &quot;mongodb memory server cannot perform operation: a background operation is currently running for collection&quot;.</p>
  <p>In another post I read something about building differents mongo instance for each block of tests.</p>
  <p>What I have so far is a globalsetup file where I start the mongo replica set like this:</p>
  <pre><code>// global.ts
  
  import { MongoMemoryReplSet } from &quot;mongodb-memory-server&quot;;
  
  const replSet = new MongoMemoryReplSet({
    replSet: { storageEngine: &quot;wiredTiger&quot; },
  });
  
  module.exports = async () =&gt; {
    await replSet.waitUntilRunning();
    const uri = await replSet.getUri();
    process.env.MONGODB_URI = uri;
  };
  </code></pre>
  <p>and my db.ts is like this</p>
  <pre><code>// db.ts
  export const connect = async () =&gt; {
    mongoose.set(&quot;useFindAndModify&quot;, false); 
    const conn = mongoose.connect(
      process.env.MONGODB_URI || config.connectionString, connectionSettings
    );
  </code></pre>
  <p>When trying to call it from a test file I do something like this</p>
  <pre><code>// test.spec.ts
  import db from &quot;./db&quot;;
  beforeAll(async () =&gt; {
    await db.connect();
  });
  afterAll(async (done) =&gt; {
    await db.dropCollections();
    await db.disconnect(done);
  });
  
  beforeEach(async () =&gt; {
    await seed();
  });
  
  describe('Some test', () =&gt; {
     it('Should not fail and get the seeders', () =&gt; {
        // some random tests using the seeds values
     })
  })
  </code></pre>
  <p>What I read in that post is instead of using globalSetup use a setupFile that will run for every test instead of one globally and then I <strong>MIGHT</strong> be able to solve the concurrency issue I have with my tests.</p>
  <p>So, to conclude, does anyone knows if there is a proper way to configure the mongodb in memory or if I am doing something THAT BAD that is allowing this to happend or if is there any kind of improvement I can do that will prevent <strong>&quot;mongodb memory server cannot perform operation: a background operation is currently running for collection&quot;</strong> this to happen?</p>