Skip to content

Instantly share code, notes, and snippets.

@ecasilla
Last active December 25, 2021 20:44
Show Gist options
  • Star 6 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ecasilla/20f3a82398f0aa97d260 to your computer and use it in GitHub Desktop.
Save ecasilla/20f3a82398f0aa97d260 to your computer and use it in GitHub Desktop.
Clear mongo db before mocha specs
var config = require('path/to/config');
var mongoose = require('mongose');
process.env.NODE_ENV = 'test';
before(function (done) {
function clearCollections() {
for (var collection in mongoose.connection.collections) {
mongoose.connection.collections[collection].remove(function() {});
}
return done();
}
if (mongoose.connection.readyState === 0) {
mongoose.connect(config.test.db, function (err) {
if (err) throw err;
return clearCollections();
});
} else {
return clearCollections();
}
});
afterEach(function (done) {
mongoose.disconnect();
return done();
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment