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.fixAlways 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.fixConsider 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.fixMigrate to a maintained alternative.
affects: *
gotchaRequires a running MongoDB instance; does not start one automatically.fixUse 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.
fixUse: 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')().
fixEnsure 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.
fixEnsure mongoose.connect completes before tests. Use the done callback in beforeEach.
Audit
Dependencies
mocharequiredPeer dependency for running tests
mongodbrequiredPeer dependency for MongoDB driver (used via mongoose)