shelfio / jest-mongodb

Jest preset for MongoDB in-memory server
MIT License
587 stars 83 forks source link

Jest runner never exits after completing tests #469

Open Floriferous opened 1 week ago

Floriferous commented 1 week ago

I'm trying to use this library to run our tests, and it works well in watch mode. However in normal mode, the runner never exits:

...rest of output

Test Suites: 2 passed, 2 total
Tests:       5 passed, 5 total
Snapshots:   0 total
Time:        11.265 s
Ran all test suites.
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.
Error: The operation was canceled.

It just hangs there forever until I manually canceled it.

When I try to run it with --detectOpenHandles, I get the following (2 exactly the same, from the 2 test suites):

Jest has detected the following 2 open handles potentially keeping Jest from exiting:

  ●  TCPWRAP

      5 |   try {
    > 6 |     await client.db('profiler-demo').collection(collectionName).deleteMany({});
        |                                                                 ^
      7 |   } catch (error) {
      8 |     console.log('Reset collection error:', error);
      9 |   }

      at makeSocket (node_modules/mongodb/src/cmap/connect.ts:356:18)
      at node_modules/mongodb/src/sdam/monitor.ts:389:36
      at checkServer (node_modules/mongodb/src/sdam/monitor.ts:401:5)
      at MonitorInterval.fn (node_modules/mongodb/src/sdam/monitor.ts:448:5)
      at MonitorInterval._executeAndReschedule (node_modules/mongodb/src/sdam/monitor.ts:694:10)
      at new MonitorInterval (node_modules/mongodb/src/sdam/monitor.ts:611:12)
      at Monitor.connect (node_modules/mongodb/src/sdam/monitor.ts:165:24)
      at Server.connect (node_modules/mongodb/src/sdam/server.ts:232:21)
      at createAndConnectServer (node_modules/mongodb/src/sdam/topology.ts:820:10)
      at node_modules/mongodb/src/sdam/topology.ts:445:9
          at Array.map (<anonymous>)
      at Topology._connect (node_modules/mongodb/src/sdam/topology.ts:443:26)
      at Topology.connect (node_modules/mongodb/src/sdam/topology.ts:408:34)
      at topologyConnect (node_modules/mongodb/src/mongo_client.ts:554:30)
      at MongoClient._connect (node_modules/mongodb/src/mongo_client.ts:566:13)
      at MongoClient.connect (node_modules/mongodb/src/mongo_client.ts:477:34)
      at executeOperation (node_modules/mongodb/src/operations/execute_operation.ts:88:20)
      at Collection.deleteMany (node_modules/mongodb/src/collection.ts:441:34)
      at src/lib/test/testUtilities.ts:6:65
      at src/lib/test/testUtilities.ts:8:71
      at Object.<anonymous>.__awaiter (src/lib/test/testUtilities.ts:4:12)
      at resetCollection (src/lib/test/testUtilities.ts:3:65)
      at src/lib/mongo/test/CollectionService.test.ts:17:26
      at src/lib/mongo/test/CollectionService.test.ts:8:71
      at Object.<anonymous>.__awaiter (src/lib/mongo/test/CollectionService.test.ts:4:12)
      at Object.<anonymous> (src/lib/mongo/test/CollectionService.test.ts:16:25)

I tried to close the connection, in a global teardown file, or in each file independently, but it never stops. What else can I try?

guilherme-gm commented 1 week ago

not sure if it helps, but I remember I had an issue where using jest's fake timers would prevent mongo operations from running and cause issues. my solution was to not use fake timers in my tests.