4

I have a micro service running with Express and save data to MongoDB using mongoose 6.x.x version. I am trying to test it with supertest and jest.

I'm setting up some hooks to create an instance of Mongo Memory Server on version 8.x.x, using the beforeAll hooks as showed in the example:

import { MongoMemoryServer } from 'mongodb-memory-server';
import mongoose from 'mongoose';
 
let mongod: any;
 
beforeAll(async () => {
  mongod = await MongoMemoryServer.create();
  const uri = mongod.getUri();
  await mongoose.connect(uri);
});

The mongod variable tries to reach a global scope.

At this point I get the error:

Starting the instance failed, enable debug for more information.

Therefore, I cannot use the instance in tests. I have enabled debug mode. And I get this errors about the mongo instance:

MongoMS:MongoInstance Mongo[52463]: start: Starting Processes +8ms
MongoMS:MongoInstance Mongo[52463]: _launchMongod: Launching Mongod Process +1ms MongoMS:MongoInstance Mongo[52463]: prepareCommandArgs +0ms MongoMS:MongoInstance Mongo[52463]: prepareCommandArgs: final argument array:["--port","52463","--dbpath","/var/folders/wt/d0s3lj915wd5pyvc9r7zdfx40000gn/T/mongo-mem--10268-fsntc7R8OJql","--storageEngine","ephemeralForTest","--bind_ip","127.0.0.1","--noauth"] +0ms MongoMS:MongoMemoryServer Mongo[unknown]: stop: Called .stop() method +23ms MongoMS:MongoMemoryServer Mongo[unknown]: stop: "instanceInfo" is not defined (never ran?) +0ms

The error is genereted because instance is not defined and I am trying to call mongod.stop in afterAll hooks to stop the instance.

Anyone has an idea of this?

4
  • Are you running your app in a docker container? Commented Mar 18, 2022 at 7:33
  • 1
    Getting this error on Fedora 36 as well Commented Jul 11, 2022 at 15:01
  • @TusharMistry is a version error. Look the answer I did myself down. Commented Jul 11, 2022 at 15:12
  • Had a problem it seems mongodb binaries problem Issue Commented Jul 12, 2022 at 14:02

3 Answers 3

3

The error was caused by an incompatibility in the architecture of my Mac. Normally the software would load, but it would not start the instance because the correct software was not running.

On Mac devices with ARM architecture, the use of Rosetta 2 is essential. As of version 7.x.x of MongoDB Memory Server the package downloads the software with correct architecture, automatically.

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

Comments

0

I had the same problem. Fixed it by adding a .dockerignore-file

# ignore all node_modules, no matter where, like when having a monorepo
**/node_modules

Comments

0

bump mongodb-memory-server package solved my issue

Comments

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.