Throwing 'You are importing a file after the Jest environment has been torn down'

Hello Internet,

I am taking Mosh’s node.js course and now learning automated testing. While doing so, an error came at random.

Here is a shortened version of the error:

ReferenceError: You are importing a file after the Jest environment has been torn down.
    at (Stack Trace)

Followed by:

TypeError: Right-hand side of instanceof is not callable
    at (Stack Trace)

It’s strange because all my tests are passing, and when I introduce a bug in my code they still pass.

I’m using windows with node 14.15.4 and npm 6.14.10 and testing environment is set to node.

Here is my complete testing code.

Could you please share the full error log? Is it pointing to some file (particularly in your project)?

Sure,

Jest has detected the following 1 open handle potentially keeping Jest from exiting:

  ●  TCPSERVERWRAP

       9 | const port = process.env.PORT || 3000
      10 | 
    > 11 | module.exports = app.listen(port, () => logger.info(`Listening on port ${port}`))
         |                      ^
      12 | 

      at Function.listen (node_modules/express/lib/application.js:618:24)
      at Object.<anonymous> (index.js:11:22)


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/mongoose/node_modules/mongodb/lib/cmap/message_stream.js:35:21)       
      at new Connection (node_modules/mongoose/node_modules/mongodb/lib/cmap/connection.js:54:28)
D:\Ethan\Coding\Node\vidly\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 (D:\Ethan\Coding\Node\vidly\node_modules\readable-stream\lib\_stream_readable.js:111:25)
    at BufferList.Readable (D:\Ethan\Coding\Node\vidly\node_modules\readable-stream\lib\_stream_readable.js:183:25)
    at BufferList.Duplex (D:\Ethan\Coding\Node\vidly\node_modules\readable-stream\lib\_stream_duplex.js:67:12) 
    at new BufferList (D:\Ethan\Coding\Node\vidly\node_modules\bl\bl.js:33:16)
    at new MessageStream (D:\Ethan\Coding\Node\vidly\node_modules\mongoose\node_modules\mongodb\lib\cmap\message_stream.js:35:21)
    at new Connection (D:\Ethan\Coding\Node\vidly\node_modules\mongoose\node_modules\mongodb\lib\cmap\connection.js:54:28)
    at D:\Ethan\Coding\Node\vidly\node_modules\mongoose\node_modules\mongodb\lib\core\connection\connect.js:36:29
    at callback (D:\Ethan\Coding\Node\vidly\node_modules\mongoose\node_modules\mongodb\lib\core\connection\connect.js:280:5)
    at Socket.connectHandler (D:\Ethan\Coding\Node\vidly\node_modules\mongoose\node_modules\mongodb\lib\core\connection\connect.js:325:5)
    at Object.onceWrapper (events.js:421:28)
    at Socket.emit (events.js:315:20)
    at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1132:10)
npm ERR! Test failed.  See above for more details.

There seems to be an issue with app.close(). You might not be closing it in some case.

Please share the beforeEach, beforeAll, afterEach, afterAll of your test files.