Keploy logo
MongoDB logo

Keploy as a MongoDB testing framework

Keploy mocks MongoDB by recording its wire protocol beneath your driver, capturing the exact documents and BSON types each query returned, then replaying them during tests. No mongod binary is downloaded, no replica set is started, and no collection is seeded.

Generate MongoDB tests free
keploy record -c "npm start"
18.4K+VS Code1.2M+300M+mocks created

What Keploy gives a MongoDB team

Captured at the MongoDB wire protocol

Keploy reads the protocol beneath your driver, so each operation replays with the BSON documents mongod actually returned, together with its cursor and write-result metadata.

  • Below the Node, PyMongo, Go, and Java drivers
  • Filters and options captured per operation
  • Cursor and write results preserved
  • Sessions and transactions replay in order
The problem

Why MongoDB integration tests slow teams down

The friction is rarely the assertions. It is everything around them — spinning up dependencies, keeping mocks honest, and repairing tests after every refactor.

Three ways to do it

Testing MongoDB: by hand, with mongodb-memory-server, or with Keploy

mongodb-memory-server runs a real mongod, and downloads one into every environment to do it. Keploy records a real server once and replays its responses with no binary and no boot.

Select any row for the full comparison, with code.

Same coverage, three costs

What you write for MongoDB vs what Keploy records

All three produce the same assertion. Only the third still passes after the next refactor without anyone editing it.

By hand2–4 hours
orderRepo.stub.js
hand-written
// Hand-written stub: BSON types are flattened to plain JS.
const orders = {
ord_1: {
// The driver would return an ObjectId here, not a string.
_id: 'ord_1',
amountMinor: 4200,
state: 'NEW',
createdAt: '2026-09-01T09:00:00Z',
},
};
 
module.exports = {
findOne: async ({ _id }) => orders[_id] ?? null,
 
// Nothing evaluates this pipeline. The answer is just typed out.
aggregate: async () => [
{ _id: 'NEW', count: 1, total: 4200 },
],
 
updateOne: async ({ _id }, { $set }) => {
Object.assign(orders[_id], $set);
return { modifiedCount: 1 };
},
};

The aggregate stub returns the expected answer regardless of the pipeline passed in, so a stage in the wrong order is undetectable.

mongodb-memory-server1–2 hours
orderRepo.memory.test.js
tool-assisted
// mongodb-memory-server: a real mongod, downloaded per environment.
const { MongoMemoryServer } = require('mongodb-memory-server');
const { MongoClient } = require('mongodb');
 
let mongod, client, db;
 
beforeAll(async () => {
// Fetches a platform-specific mongod binary on a cold cache.
mongod = await MongoMemoryServer.create();
client = await MongoClient.connect(mongod.getUri());
db = client.db('orders');
await db.collection('orders').insertMany(seedDocuments);
});
 
afterAll(async () => {
await client.close();
await mongod.stop();
});
 
it('summarises orders by state', async () => {
const result = await db.collection('orders').aggregate([
{ $match: { state: 'NEW' } },
{ $group: { _id: '$state', total: { $sum: '$amountMinor' } } },
]).toArray();
 
expect(result[0].total).toBe(4200);
});

Real aggregation semantics — and a binary download, a server boot, and a seed step before the first assertion in every environment.

With Keploy~5 minutes
mocks.yaml
auto-generated
# Recorded with: keploy record -c 'npm start'
# The MongoDB response, captured at the wire protocol.
version: api.keploy.io/v1beta1
kind: Mongo
name: mock-3
spec:
request:
collection: orders
operation: find
filter:
_id: "ObjectId('66f1a2c4e1b2c3d4e5f60718')"
response:
# BSON types preserved exactly as the server sent them.
documents:
- _id: "ObjectId('66f1a2c4e1b2c3d4e5f60718')"
amountMinor: "NumberLong(4200)"
state: "NEW"
createdAt: "ISODate('2026-09-01T09:00:00Z')"
cursor_id: 0
ok: 1

ObjectId, NumberLong, and ISODate replay as the BSON types mongod sent, so driver conversion is exercised rather than assumed.

Times are estimates for authoring one endpoint’s coverage from scratch, not measurements.

Keploy vs the alternatives

MongoDB testing tools, compared

The options a team on document and wide-column stores actually reaches for, and where each one genuinely wins. Select a row for the full comparison.

Best in classStrongPartialNot covered

Assessments reflect each tool’s documented behaviour, not benchmark measurements.

How it works

Record your MongoDB app once, replay it forever

Keploy sits below your MongoDB process at the network layer. It watches the calls your app already makes, then serves them back on replay so tests run with no dependencies attached.

Keploy records a GET call to /api/v1/orders/{id} on a MongoDB service and captures the dependency calls it makes.

MongoDB logo
Your MongoDB app
GET/api/v1/orders/{id}200
RecordingKeploy proxyeBPF · userspace
+4 more MongoDB clients
An example shape of a captured call. Your own endpoints and dependencies come from your real traffic, so nothing here has to be written by hand.
Mock coverage

MongoDB clients Keploy records, driver by driver

Keploy captures MongoDB at the wire protocol, so 6 of these 8 clients need no adapter, no test double, and no MongoDB instance in CI.

Quick start

Your first MongoDB test suite in under five minutes

Every command below runs against your existing MongoDB service. Nothing in your source tree changes.

  1. 1Install the Keploy CLI

    One binary on the recording machine. Nothing is installed into MongoDB and no driver wrapper is needed.

    curl -sSL https://keploy.io/install.sh | bash
  2. 2Record with your development MongoDB reachable

    Run the app as usual. Every find, insert, update, and aggregate is captured with the documents mongod returned.

    keploy record -c "npm start"
  3. 3Exercise the paths that hit the database

    Aggregation pipelines, transactions, and cursor-based reads all record with their filters and responses.

    curl localhost:3000/orders/ord_1
    curl localhost:3000/orders/summary?state=NEW
  4. 4Replay with MongoDB stopped

    Shut mongod down and run the suite. If replay passes, every operation your code performs is answered by a recorded mock.

    keploy test -c "npm start" --delay 10

Ready to try it on your own MongoDB service?

Ecosystem

Works with the rest of your MongoDB stack

Keploy records at the network layer, so framework and driver choices inside your MongoDB app do not change how it captures traffic.

FAQ

MongoDB testing with Keploy: common questions

Join our GlobalCommunity

Connect with developers worldwide. Follow updates, ask questions, share feedback, and ship faster with other Keploy builders.

1.2M+Installs
18.4K+GitHub
100K+Devs
300M+Mocks
1K+Contributors
#1OSS Trending
4.9★★★★★from 500+ reviews onG2GartnerVS CodeChrome
★★★★★

Best report of integration and API tests I've seen — which we don't get from RestAssured.

G2
★★★★★

Future of microservices testing. I don't write tests now!

G2 · 5/5
★★★★★

An amazing product that simplifies the automation.

Gartner · 4.0
XGitHubSlackYouTubeLinkedIn
Built by developers, for developers.Let's build the future, together.