10

I am trying to unit test some typescript logic with jest. I am using mongoose in order to interact with the mongo database and mongo-memory-server in order to mock mongodb.

For unclear reason, I get the following error, which seems to result from cyclic dependencies, when running the below unit test.

Error:

  ● Test suite failed to run

    RangeError: Maximum call stack size exceeded

      at Object.get [as ObjectId] (node_modules/mongoose/node_modules/mongodb/src/bson.ts:38:3)
      at Object.get [as ObjectId] (node_modules/mongoose/node_modules/mongodb/src/bson.ts:38:3)

Unit Test:

import mongoose, { Mongoose } from "mongoose";
import { MongoMemoryServer } from "mongodb-memory-server";

describe("test mongo memory server", () => {
    let client: Mongoose;
    let mongod;

    beforeAll(async () => {
      mongod = await MongoMemoryServer.create();
      client = await mongoose.connect(mongod.getUri());
    });

    afterAll(async () => {

      if (mongod) {
        await mongod.stop();
      }
    });

    it("empty test", async () => {
    });
});

Used Versions in package.json:

...  "dependencies": {
        "typescript": "4.6.3",
        "mongoose": "6.2.9", // required for mongo 5.0
     },
     "devDependencies": {
        "jest": "27.5.1",
        "mongodb-memory-server": "8.4.2",
     }

P.S:

  1. When commenting out the connection to mongo (client = await mongoose.connect(mongod.getUri());), and using mongo-memory-server (version: 7.6.3) the below unit test passes. On the other hand, when the line is not commented out, it fails.
  2. When commenting out the connection to mongo (client = await mongoose.connect(mongod.getUri());), but using latest mongo-memory-server, version 8.4.2 - the test fails.
1
  • I'm also getting the same error message when I try to test a route with jest. Did you have any luck solving this issue? Commented May 17, 2022 at 15:11

2 Answers 2

1

For me it was some errors in my jest.config.js. I had this line in it:

moduleDirectories: ["node_modules", "src"], 

When I commented it out, the error message stopped appearing.

Sign up to request clarification or add additional context in comments.

Comments

0

I've been facing the same issue today, I just removed node_modules and package-lock.json, npm install'ed and the issue was gone. Take care.

2 Comments

Thanks! unfortunately it didn't help...
This worked for me. I only needed to remove package-lock.json file

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.