Last active
August 16, 2018 21:04
-
-
Save artokun/6ee24125346695bf1815deea09f0fe08 to your computer and use it in GitHub Desktop.
Medium-Jest-setup.js
This file contains 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
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