Registry / testing / mocha-mongoose

mocha-mongoose

JSON →
library1.2.0jsnpmunverified

Test helpers for clearing MongoDB collections between Mocha test runs using Mongoose. Version 1.2.0, stable but unmaintained since 2016. Automatically drops all collections before each spec to ensure test isolation. Supports optional `skip` array to preserve specific collections and `noClear` option for manual clearing. Requires peer dependencies mocha >=1.0.0 and mongodb >=1.0.0. An alternative to mongodb-memory-server or jest-mongodb for integration testing with your local database.

npm install mocha-mongoose
INSTALL
IMPORT
SIG · MOCHA-MONGOOSE
M
mocha-mongoose
testingjavascriptv1.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.

default
var clearDB = require('mocha-mongoose')(uri);
const { clearDB } = require('mocha-mongoose');
The module exports a function that returns a clearDB function. Must be called immediately with a URI.
default with options
var clearDB = require('mocha-mongoose')(uri, { skip: ['users'] });
const clearDB = require('mocha-mongoose').init(uri, opts);
Options object can include `skip` (array of collection names) and `noClear` (boolean).
Manual clearing
var clearDB = require('mocha-mongoose')(uri, { noClear: true });
var clearDB = require('mocha-mongoose')(uri).noClear;
With `noClear`, the clearing function must be manually called to clear the database.

Shows basic setup: require mocha-mongoose, connect Mongoose in beforeEach, and verify DB is cleared before each test.

var mongoose = require('mongoose'); var clearDB = require('mocha-mongoose')('mongodb://localhost/test'); beforeEach(function(done) { if (mongoose.connection.db) return done(); mongoose.connect('mongodb://localhost/test', done); }); it('should clear the database', function(done) { var Dummy = mongoose.model('Dummy', new mongoose.Schema({a: Number})); new Dummy({a: 1}).save(function() { Dummy.count(function(err, count) { if (err) return done(err); count.should.equal(0); // Because beforeEach cleared collections done(); }); }); });
Debug
Known issues
breakingAutomatic clearing may delete all data in the connected database. Ensure a dedicated test database is used.
fix
Always use a test-specific MongoDB URI, e.g., 'mongodb://localhost/test-db'.
affects: *
gotchaLibrary is unmaintained and may not work with newer versions of Mongoose or MongoDB driver.
fix
Consider using mongodb-memory-server or jest-mongodb for automatic test databases.
affects: >=1.3.0 (Mongoose >4.x)
deprecatedPackage has no updates since 2016; last release 1.2.0.
fix
Migrate to a maintained alternative.
affects: *
gotchaRequires a running MongoDB instance; does not start one automatically.
fix
Use mongodb-memory-server for an in-memory MongoDB instance.
affects: *
Errors
Common errors & fixes
TypeError: clearDB is not a function
Importing the module incorrectly as a named export.
fix
Use: var clearDB = require('mocha-mongoose')(uri);
MongooseError: The `uri` parameter to `openUri()` must be a string, got "undefined"
Missing or undefined MongoDB URI when calling require('mocha-mongoose')().
fix
Ensure you pass a valid connection string, e.g., require('mocha-mongoose')('mongodb://localhost/test');
MongoError: topology was destroyed
Mongoose connection closed prematurely, possibly due to async issues in beforeEach.
fix
Ensure mongoose.connect completes before tests. Use the done callback in beforeEach.
Upgrade
Version history
1.2.0latest on npm
Audit
Dependencies
mocharequiredPeer dependency for running tests
mongodbrequiredPeer dependency for MongoDB driver (used via mongoose)
Agent activity
11 hits · last 30 days
node
10
Resources
mocha-mongoose — npm install mocha-mongoose · libregistry