`I am tested my node.js microservices using Jest, supertest and using mongodb-memory-server for storage. I have written the config files as per the latest guides of jest and mongodb-memory-server but once I start the Test, I am getting below Error.
**Starting the MongoMemoryServer Instance failed**, enable debug log for more information. Error:
# **Error: spawn Unknown system error -86**
at ChildProcess.spawn (node:internal/child_process:413:11)
at spawn (node:child_process:757:9)
at MongoInstance._launchMongod (/Users/aadilnabi/web/microservices/ticketing/auth/node_modules/mongodb-memory-server-core/src/util/MongoInstance.ts:474:31)
at MongoInstance.<anonymous> (/Users/aadilnabi/web/microservices/ticketing/auth/node_modules/mongodb-memory-server-core/src/util/MongoInstance.ts:376:31)
at Generator.next (<anonymous>)
at fulfilled (/Users/aadilnabi/web/microservices/ticketing/auth/node_modules/tslib/tslib.js:164:62) {
errno: -86,
code: 'Unknown system error -86',
syscall: 'spawn'
}
at MongoMemoryServer.<anonymous> (node_modules/mongodb-memory-server-core/src/MongoMemoryServer.ts:293:17)
at node_modules/tslib/tslib.js:167:75
at __awaiter (node_modules/tslib/tslib.js:163:16)
at node_modules/mongodb-memory-server-core/src/MongoMemoryServer.ts:284:68
FAIL src/routes/__test__/signup.test.ts
✕ returns 201 on a successful signup
● returns 201 on a successful signup
spawn Unknown system error -86
at MongoInstance._launchMongod (node_modules/mongodb-memory-server-core/src/util/MongoInstance.ts:474:31)
at MongoInstance.<anonymous> (node_modules/mongodb-memory-server-core/src/util/MongoInstance.ts:376:31)
at fulfilled (node_modules/tslib/tslib.js:164:62)
Test Suites: 1 failed, 1 total
Tests: 1 failed, 1 total
Snapshots: 0 total
Time: 2.21 s
Ran all test suites.
Watch Usage
› Press f to run only failed tests.
› Press o to only run tests related to changed files.
› Press p to filter by a filename regex pattern.
› Press t to filter by a test name regex pattern.
› Press q to quit watch mode.
› Press i to run failing tests interactively.
› Press Enter to trigger a test run.
/Users/aadilnabi/web/microservices/ticketing/auth/node_modules/mongodb-memory-server-core/lib/util/MongoInstance.js:150
const err = new errors_1.GenericMMSError(`Instance failed to start within ${timeoutTime}ms`);
^
GenericMMSError: **Instance failed to start within 10000ms**
at Timeout.<anonymous> (/Users/aadilnabi/web/microservices/ticketing/auth/node_modules/mongodb-memory-server-core/src/util/MongoInstance.ts:365:21)
at listOnTimeout (node:internal/timers:569:17)
at processTimers (node:internal/timers:512:7)
Node.js v18.14.2
My dev dependecies are as below:
"devDependencies": {
"@types/jest": "^29.5.0",
"@types/supertest": "^2.0.12",
"jest": "^29.5.0",
"mongodb-memory-server": "^8.12.2",
"supertest": "^6.3.3",
"ts-jest": "^29.1.0"
}
Config file (setup.ts) contains below:
jest.setTimeout(10000);
mongo = await MongoMemoryServer.create();
const mongoUri = mongo.getUri();
await mongoose.connect(mongoUri);
});
beforeEach(async () => {
const collections = await mongoose.connection.db.collections();
for (let collection of collections) {
await collection.deleteMany({});
}
});
afterAll(async () => {
if (mongo) {
await mongo.stop();
}
await mongoose.connection.close();
});
Solution:
Looks like Mongodb-memory-server needs Rosetta 2 on Mac OS. Once I installed Rosetta on my Mac, it solved this issue and I was able to perform my test successfully.