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?
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.