You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

MongoMemoryServer启动失败报错spawn Unknown system error -86求助

问题:MongoMemoryServer启动失败,报错spawn Unknown system error -86及超时

使用Jest、supertest和mongodb-memory-server测试Node.js微服务,已按最新指南完成配置,但启动测试时出现Starting the MongoMemoryServer Instance failed错误,具体为Error: spawn Unknown system error -86,同时伴随Instance failed to start within 10000ms超时错误。

错误日志

**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

开发依赖

"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"
  }

setup.ts配置

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();
});

解决方案

1. 错误根源

Unknown system error -86在Apple Silicon芯片Mac上,大多是架构不兼容导致:mongodb-memory-server默认下载的MongoDB二进制文件为x86架构,无法在arm64架构的机器上运行。

2. 修复步骤

  • 方案一:用Rosetta运行终端
    找到终端应用,右键选择「显示简介」,勾选「使用Rosetta打开」,重启终端后重新执行测试。

  • 方案二:强制指定arm64架构二进制文件
    在项目根目录创建.mongodb-memory-server.json文件,写入以下内容:

    {
      "arch": "arm64",
      "platform": "darwin"
    }
    

    删除node_modules和package-lock.json(或yarn.lock),重新执行npm install后运行测试。

  • 方案三:升级mongodb-memory-server版本
    升级到^8.13.0及以上版本,该版本已修复Apple Silicon架构兼容问题,执行命令:

    npm install mongodb-memory-server@latest --save-dev
    
  • 方案四:延长超时时间
    将setup.ts中的jest.setTimeout(10000);改为jest.setTimeout(30000);,避免因二进制文件下载过慢触发超时。


内容的提问来源于stack exchange,提问作者Aadil Nabi

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.24 19:40:29