title: "ReferenceError: You are trying to `import` a file after the Jest environment has been torn down" when using jest with mongoose
body: <p>i have two test files that connect with mongoose, the problem started when i added the second one
  i tried to use <code>jest.useFakeTimers()</code> after imports but nothing changed</p>
  <p><em>here is the test file</em></p>
  <pre><code>const mongoose = require(&quot;mongoose&quot;);
  const supertest = require(&quot;supertest&quot;);
  const app = require(&quot;../app&quot;);
  const api = supertest(app);
  const User = require(&quot;../models/user&quot;);
  const bcrypt = require(&quot;bcryptjs&quot;);
  
  describe(&quot;adding users&quot;, () =&gt; {
    beforeEach(async () =&gt; {
      await User.deleteMany({});
  
      const passwordHash = await bcrypt.hash(&quot;sekret&quot;, 10);
      const user = new User({ username: &quot;root&quot;, passwordHash });
  
      await user.save();
    });
    test(&quot;adding users successfuly&quot;, async () =&gt; {
      const newUser = {
        name: &quot;john oliver&quot;,
        username: &quot;karana&quot;,
        password: &quot;ooiiu&quot;,
      };
      await api.post(&quot;/api/users&quot;).send(newUser).expect(200);
    });
  });
  
  afterAll(() =&gt; {
    mongoose.connection.close();
  });
  
  </code></pre>
  <p><em>and here is the mongoose model</em></p>
  <pre><code>const mongoose = require(&quot;mongoose&quot;);
  const uniqueValidator = require(&quot;mongoose-unique-validator&quot;);
  
  const userSchema = new mongoose.Schema({
    name: String,
    username: {
      type: String,
      unique: true,
      minlength: 3,
    },
    passwordHash: String,
  });
  userSchema.plugin(uniqueValidator);
  
  userSchema.set(&quot;toJSON&quot;, {
    transform: (document, returnedObject) =&gt; {
      returnedObject.id = returnedObject._id.toString();
      delete returnedObject._id;
      delete returnedObject.__v;
      delete returnedObject.passwordHash;
    },
  });
  
  module.exports = mongoose.model(&quot;User&quot;, userSchema);
  </code></pre>
  <p><em>here is the controller</em></p>
  <pre><code>const bcrypt = require(&quot;bcryptjs&quot;);
  const usersRouter = require(&quot;express&quot;).Router();
  const User = require(&quot;../models/user&quot;);
  
  usersRouter.get(&quot;/&quot;, async (req, res) =&gt; {
    const users = await User.find({});
    res.json(users);
  });
  
  usersRouter.post(&quot;/&quot;, async (req, res, next) =&gt; {
    const body = req.body;
  
    if (body.password.length &gt;= 3) {
      const saltRounds = 10;
      const passwordHash = await bcrypt.hash(body.password, saltRounds);
  
      const user = new User({
        name: body.name,
        username: body.username,
        passwordHash,
      });
  
      try {
        const result = await user.save();
        res.status(200).json(result);
      } catch (err) {
        next(err);
      }
    } else {
      res
        .status(400)
        .json({ error: &quot;password must be atleast 3 charecters long&quot; });
    }
  });
  
  module.exports = usersRouter;
  
  </code></pre>
  <p>*and there router is * <code>app.use(&quot;/api/users&quot;, usersRouter);</code> as written in the <code>app.js</code> files.</p>
  <p><em><strong>here is the error am getting</strong></em></p>
  <pre><code>ReferenceError: You are trying to `import` a file after the Jest environment has been torn down.
  
        at BufferList.Readable (node_modules/readable-stream/lib/_stream_readable.js:179:22)
        at BufferList.Duplex (node_modules/readable-stream/lib/_stream_duplex.js:67:12)
        at new BufferList (node_modules/bl/bl.js:33:16)
        at new MessageStream (node_modules/mongodb/lib/cmap/message_stream.js:35:21)
        at new Connection (node_modules/mongodb/lib/cmap/connection.js:54:28)
  C:\Users\oussama\Desktop\web-projects\fullstackopen-part4\node_modules\readable-stream\lib\_stream_readable.js:111
    var isDuplex = stream instanceof Duplex;
                          ^
  
  TypeError: Right-hand side of 'instanceof' is not callable
      at new ReadableState (C:\Users\oussama\Desktop\web-projects\fullstackopen-part4\node_modules\readable-stream\lib\_stream_readable.js:111:25)
      at BufferList.Readable (C:\Users\oussama\Desktop\web-projects\fullstackopen-part4\node_modules\readable-stream\lib\_stream_readable.js:183:25)
      at BufferList.Duplex (C:\Users\oussama\Desktop\web-projects\fullstackopen-part4\node_modules\readable-stream\lib\_stream_duplex.js:67:12)
      at new BufferList (C:\Users\oussama\Desktop\web-projects\fullstackopen-part4\node_modules\bl\bl.js:33:16)
      at new MessageStream (C:\Users\oussama\Desktop\web-projects\fullstackopen-part4\node_modules\mongodb\lib\cmap\message_stream.js:35:21)
      at new Connection (C:\Users\oussama\Desktop\web-projects\fullstackopen-part4\node_modules\mongodb\lib\cmap\connection.js:54:28)
      at C:\Users\oussama\Desktop\web-projects\fullstackopen-part4\node_modules\mongodb\lib\core\connection\connect.js:36:29
      at callback (C:\Users\oussama\Desktop\web-projects\fullstackopen-part4\node_modules\mongodb\lib\core\connection\connect.js:280:5)
      at TLSSocket.connectHandler (C:\Users\oussama\Desktop\web-projects\fullstackopen-part4\node_modules\mongodb\lib\core\connection\connect.js:325:5)
      at Object.onceWrapper (events.js:421:28)
      at TLSSocket.emit (events.js:315:20)
      at TLSSocket.onConnectSecure (_tls_wrap.js:1530:10)
      at TLSSocket.emit (events.js:315:20)
      at TLSSocket._finishInit (_tls_wrap.js:936:8)
      at TLSWrap.ssl.onhandshakedone (_tls_wrap.js:710:12)
  
  </code></pre>
  <p>the error happen after saving the data (users) to mongoDB.
  thanks in advance.</p>