typegoose / typegoose/mongodb-memory-server

Error when running with mongodb node driver 7.6 in Jest

Open
#1,026 3 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

bug has repro script
Dominant language
TypeScript
Stars
2.8k
Forks
191
Avg merge
4h 25m
Merged PRs (30d)
10

Description

Versions

  • NodeJS: 22.x
  • mongodb-memory-server-*: 11.2.0
  • mongodb(the binary version): 8.2.0
  • mongodb(the js package): 7.6.0
  • system: Ubuntu 24.04

package: mongo-memory-server

What is the Problem?

If installing mongodb-memory-server with mongodb node driver 7.6.0, running in Jest fails with the following error:

    MongoServerSelectionError: Missing required sub-document 'driver' in the client metadata document

       6 |   it('connects successfully to a replica set started by mongodb-memory-server', async () => {
       7 |     console.log('Creating replica set...');
    >  8 |     replSet = await MongoMemoryReplSet.create({ replSet: { count: 1 } });
         |               ^
       9 |     console.log('Replica set created, connecting mongoose...');
      10 |     const uri = replSet.getUri();
      11 |

      at Topology.selectServer (node_modules/mongodb/src/sdam/topology.ts:638:30)
      at Topology._connect (node_modules/mongodb/src/sdam/topology.ts:466:22)
      at Topology.connect (node_modules/mongodb/src/sdam/topology.ts:402:7)
      at topologyConnect (node_modules/mongodb/src/mongo_client.ts:684:9)
      at MongoClient._connect (node_modules/mongodb/src/mongo_client.ts:696:7)
      at MongoClient.connect (node_modules/mongodb/src/mongo_client.ts:608:7)
      at Function.connect (node_modules/mongodb/src/mongo_client.ts:887:12)
      at MongoMemoryReplSet._initReplSet (node_modules/mongodb-memory-server-core/src/MongoMemoryReplSet.ts:669:30)
      at MongoMemoryReplSet.start (node_modules/mongodb-memory-server-core/src/MongoMemoryReplSet.ts:406:5)
      at Function.create (node_modules/mongodb-memory-server-core/src/MongoMemoryReplSet.ts:207:5)
      at Object.<anonymous> (repro.spec.ts:8:15)

More details here: https://github.com/Automattic/mongoose/issues/16499 and this repo: https://github.com/andreglock/mongodb-7.6-hanshake-bug

Code Example

import { MongoMemoryReplSet } from 'mongodb-memory-server';

describe('mongoose 9.9.4 + mongodb handshake regression', () => {
  let replSet: MongoMemoryReplSet;

  it('connects successfully to a replica set started by mongodb-memory-server', async () => {
    console.log('Creating replica set...');
    replSet = await MongoMemoryReplSet.create({ replSet: { count: 1 } });
    console.log('Replica set created, connecting mongoose...');
    const uri = replSet.getUri();

    console.log('Got uri', uri);
  }, 120_000); // increased timeout to rule out slow binary download/replset init

  afterAll(async () => {
    if (replSet) {
      await replSet.stop();
    }
  });
});

Jest config:

/** @type {import('jest').Config} */
module.exports = {
  preset: 'ts-jest',
  testEnvironment: 'node',
  testTimeout: 30000,
};

package.json:

{
  "name": "mongoose-mongodb-repro",
  "version": "1.0.0",
  "private": true,
  "type": "commonjs",
  "scripts": {
    "test": "jest"
  },
  "devDependencies": {
    "jest": "^29.7.0",
    "ts-jest": "^29.2.5",
    "typescript": "^5.6.3",
    "@types/jest": "^29.5.14",
    "mongodb-memory-server": "^11.2.0"
  },
  "dependencies": {
    "mongoose": "9.9.4"
  }
}

Debug Output

Debug Output
Put The Debugout output here

Debug output here: https://gist.github.com/vkarpov15/660678940bd7b5020fd6137d949b63fb

Do you know why it happenes?

There's some more info here: https://github.com/Automattic/mongoose/issues/16499

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by running the supplied Jest reproduction in repro.spec.ts with the listed Node.js, mongodb-memory-server, MongoDB, and driver versions. Read MongoMemoryReplSet.ts around _initReplSet and compare the handshake reports linked in the issue. Done means replica-set creation succeeds with mongodb driver 7.6 and the regression is covered by a test.

Written by the indexing model from the issue text.

Assessment

Tech stack
mongodb, typescript
Domain
database, testing
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Active
Clarity
Mostly clear
Newbie friendliness
52/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.