Skip to content

Instantly share code, notes, and snippets.

@alexandrusavin
Created September 30, 2015 17:36
Show Gist options
  • Save alexandrusavin/318e0a7593c1a8cbe94f to your computer and use it in GitHub Desktop.
Save alexandrusavin/318e0a7593c1a8cbe94f to your computer and use it in GitHub Desktop.
root mocha setup for mongodb int tests
'use strict';
var mongoose = require('mongoose');
var _ = require('lodash');
var conn;
function clearDB() {
var cleaned = [];
_.each(conn.collections, (function (col) {
cleaned.push(col.remove());
}));
return Promise.all(cleaned);
}
beforeEach(function () {
if (!conn) {
conn = mongoose.createConnection('mongodb://localhost:21017/f2test');
} else if (conn.readyState === 0) {
conn.on('open', function (err) {
if (err) {
throw err;
}
return clearDB();
});
} else {
return clearDB();
}
});
afterEach(function (done) {
conn.close();
return done();
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment