Registry / testing / mongo-mock

mongo-mock

JSON →
library4.2.0jsnpmunverified

Mongo-Mock (v4.2.0, last released 2022) is an in-memory mock implementation of the MongoDB server, designed to mimic the official MongoDB Node.js driver API. It allows developers to test MongoDB interactions without a real database, supporting basic CRUD operations, async simulation with configurable delay, optional disk persistence, and some query operators. Compared to alternatives like mongodb-memory-server, it is lighter-weight but has limited feature coverage and is no longer actively maintained.

npm install mongo-mock
INSTALL
IMPORT
SIG · MONGO-MOCK
M
mongo-mock
testingjavascriptv4.2.0
harness data pending
Install & Compatibility
Where this runs

No compatibility data collected yet for this library.

Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

MongoClient
import { MongoClient } from 'mongo-mock'
const MongoClient = require('mongodb').MongoClient
mongo-mock exports a top-level MongoClient, not from 'mongodb' package. Use ES import or require('mongo-mock') directly.
default import
import mongodb from 'mongo-mock'
import * as mongodb from 'mongo-mock'
Default import gives an object with MongoClient, etc. Namespace import is not needed.
require CommonJS
const mongodb = require('mongo-mock')
const { MongoClient } = require('mongo-mock')
Destructuring from require works, but the entire object is also fine. The package supports both CJS and ESM.

Connect to an in-memory mock, insert documents, and query them using async callbacks.

import { MongoClient } from 'mongo-mock'; // optional: set to 0 for synchronous behavior (default delay is 400ms) MongoClient.max_delay = 0; // optional: persist data to disk MongoClient.persist = 'mongo.json'; const url = 'mongodb://localhost:27017/myproject'; MongoClient.connect(url, {}, (err, client) => { if (err) { console.error(err); return; } const db = client.db(); const collection = db.collection('documents'); collection.insertMany([{ a: 1 }, { a: 2 }, { a: 3 }], (err, result) => { console.log('inserted', result); collection.findOne({ a: 2 }, (err, doc) => { console.log('found', doc); db.close(); }); }); });
Debug
Known issues
gotchaDefault async delay of 400ms can cause slow tests if not set to 0.
fix
Set MongoClient.max_delay = 0 for synchronous behavior.
affects: >=1.0.0
gotchaNot all MongoDB features are implemented; advanced queries (aggregation, geospatial) may fail silently.
fix
Check the GitHub issues or README for supported features. Use a different mock for complex pipelines.
affects: >=1.0.0
breakingIn v4, the API changed from callback-only to support promises, but callbacks are still default.
fix
Ensure you handle both callbacks and promises correctly. If using promises, omit the callback parameter.
affects: >=4.0.0 <5.0.0
deprecatedThe package is no longer actively maintained; last release 2022.
fix
Consider migrating to mongodb-memory-server for active support.
affects: >=4.0.0
Errors
Common errors & fixes
MongoClient is not a constructor
Using import/require incorrectly, e.g., importing default as class.
fix
Use: const { MongoClient } = require('mongo-mock');
Cannot find module 'mongo-mock'
Package not installed or misspelled.
fix
Run: npm install mongo-mock
TypeError: db.collection is not a function
Using the old driver API where db is the client; in mongo-mock v4, client.db() returns a database object.
fix
Call client.db() to get the db object before using collection.
Timeout - Async callback was not invoked
Test framework expecting callback but mongo-mock's default delay (400ms) may exceed timeout.
fix
Set MongoClient.max_delay = 0 or increase test timeout.
Upgrade
Version history
4.2.0latest on npm
Audit
Dependencies

No dependency data recorded yet.

Agent activity
9 hits · last 30 days
node
8
Resources
mongo-mock — npm install mongo-mock · libregistry