Last active
December 25, 2021 20:44
-
-
Save ecasilla/20f3a82398f0aa97d260 to your computer and use it in GitHub Desktop.
Clear mongo db before mocha specs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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