'Receiving error when clearing collections in Jest

I trying to stick to the DRY principle by using a jest setup file where I connect to a database in the "beforeAll" hook, clear data in collections in the "beforeEach" hook and finally close connections in "afterAll.

When resetting data from the collection, I see that all test passes but under the coverage table I see an error message which indicated the following:

/Volumes/Seagate/microservices/ticketing/auth/node_modules/mongoose/lib/drivers/node-mongodb-native/collection.js:151
          const err = new MongooseError(message);
                      ^

MongooseError: Operation `users.deleteMany()` buffering timed out after 10000ms
    at Timeout.<anonymous> (/Volumes/Seagate/Tutorials/Udemy/NodeJs/microservices/ticketing/auth/node_modules/mongoose/lib/drivers/node-mongodb-native/collection.js:151:23)
    at listOnTimeout (node:internal/timers:559:17)
    at processTimers (node:internal/timers:502:7)

This is my code for resseting the collections:

exports.resetData = async () => {
  const collections = await mongoose.connection.collections;

  for (let collection in collections) {
    collections[collection].deleteMany({});
  }
};

Can someone explain to me what silly mistake I'm doing?

Thank you in advanced!!!



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source