mongoose: MongoNotConnectedError on test using mongodb-memory-server and jest

Do you want to request a feature or report a bug? bug

What is the current behavior?

    MongoNotConnectedError: MongoClient must be connected to perform this operation

      at getTopology (node_modules/mongoose/node_modules/mongodb/src/utils.ts:451:9)
      at Collection.find (node_modules/mongoose/node_modules/mongodb/src/collection.ts:762:18)
      at Collection.findOne (node_modules/mongoose/node_modules/mongodb/src/collection.ts:740:17)
      at NativeCollection.<computed> [as findOne] (node_modules/mongoose/lib/drivers/node-mongodb-native/collection.js:202:33)
      at NodeCollection.findOne (node_modules/mquery/lib/collection/node.js:34:21)
      at model.Query.Object.<anonymous>.Query.findOne (node_modules/mquery/lib/mquery.js:2021:20)
      at model.Query.<anonymous> (node_modules/mongoose/lib/query.js:2435:22)
      at model.Query._wrappedThunk [as _findOne] (node_modules/mongoose/lib/helpers/query/wrapThunk.js:27:8)
      at node_modules/kareem/index.js:372:33

If the current behavior is a bug, please provide the steps to reproduce.

The error happen on one test out of 7 and sometime it passes so I guess their is a race condition somewhere. Maybe related to #11445

What is the expected behavior? no error

What are the versions of Node.js, Mongoose and MongoDB you are using? Note that “latest” is not a version.

jest setupFilesAfterEnv (if you have suggestion to improve this part please tell me):

import mongoose from "mongoose";
import { MongoMemoryReplSet } from "mongodb-memory-server-core";

let replicaSet: MongoMemoryReplSet;

jest.setTimeout(30000);

beforeAll(async () => {
  replicaSet = await MongoMemoryReplSet.create({
    replSet: { storageEngine: "wiredTiger" },
  });
  const uri = replicaSet.getUri();
  await mongoose.connect(uri);
});

afterEach(async () => {
  const collections = await mongoose.connection.db.collections();
  for (const collection of collections) {
    await collection.deleteMany({});
  }
});

afterAll(async () => {
  await mongoose.connection.db.dropDatabase();
  await mongoose.disconnect();
  await replicaSet.stop();
});

Please note I use a change stream UserModel.watch().on("change" ...)

mongoose 6.2.10 mongodb 4.4.13 mongodb-memory-server-core 8.4.2 jest 27.5.1

About this issue

  • Original URL
  • State: closed
  • Created 5 years ago
  • Comments: 17 (5 by maintainers)

Commits related to this issue

Most upvoted comments

Thanks @nodkz 👍

@root-io I would be remiss if I didn’t warn you about the dangers of testing with Jest. We highly recommend using any other test runner. Jest is not a test framework, it is an alternative JavaScript runtime. Testing an app with Jest but running it in Node is like testing the app in the browser but running it in Node - you’ll get failures in test that don’t happen in prod, and failures in prod that don’t happen in test.

@sibelius are you using Jest fake timers? That’s a likely culprit, MongoDB Node driver’s internal replica set monitoring logic makes heavy use of setTimeout() and setInterval().

No idea. @sibelius are you running into a similar issue with setupFilesAfterEnv? Looks like Jest keeps on producing new ridiculous antipatterns for us to work around 😞