Skip to content

Instantly share code, notes, and snippets.

@suhas86
Created July 5, 2020 06:10
Show Gist options
  • Save suhas86/e504f4958e8edb5fa0c0bef23d6c167d to your computer and use it in GitHub Desktop.
Save suhas86/e504f4958e8edb5fa0c0bef23d6c167d to your computer and use it in GitHub Desktop.
import { MongoMemoryServer } from "mongodb-memory-server";
import mongoose from "mongoose";
let mongo: any;
// Start mongo server before start running test
beforeAll(async () => {
mongo = new MongoMemoryServer();
const mongoUri = await mongo.getUri();
await mongoose.connect(mongoUri, {
useNewUrlParser: true,
useUnifiedTopology: true,
});
});
// Before each test reset collections
beforeEach(async () => {
const collections = await mongoose.connection.db.collections();
for (let collection of collections) {
await collection.deleteMany({});
}
});
// Close connection after completing the testing process
afterAll(async () => {
await mongo.stop();
await mongoose.connection.close();
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment