Skip to content

Instantly share code, notes, and snippets.

@artokun
Last active August 16, 2018 21:04
Show Gist options
  • Save artokun/6ee24125346695bf1815deea09f0fe08 to your computer and use it in GitHub Desktop.
Save artokun/6ee24125346695bf1815deea09f0fe08 to your computer and use it in GitHub Desktop.
Medium-Jest-setup.js
const mongoose = require('mongoose');
// Load models since we will not be instantiating our express server.
require('../models/System');
require('../models/Celestial');
beforeEach(function(done) {
/*
Define clearDB function that will loop through all
the collections in our mongoose connection and drop them.
*/
function clearDB() {
for (var i in mongoose.connection.collections) {
mongoose.connection.collections[i].remove(function() {});
}
return done();
}
/*
If the mongoose connection is closed,
start it up using the test url and database name
provided by the node runtime ENV
*/
if (mongoose.connection.readyState === 0) {
mongoose.connect(
`mongodb://localhost:27017/${process.env.TEST_SUITE}`, // <------- IMPORTANT
function(err) {
if (err) {
throw err;
}
return clearDB();
}
);
} else {
return clearDB();
}
});
afterEach(function(done) {
mongoose.disconnect();
return done();
});
afterAll(done => {
return done();
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment