I'm doing some e2e tests of a nodeJS application. In the before/after hooks I need to add/remove some mongoDB documents and this is how I do that:
Shouldn't it be possible to connect only one time to the mongo server at all?
What I would like to do:
- Remove all documents
{ _id: articleId }at the beginning (right now missing in the code) - Insert new document to DB (
collection.insertOne(articles.main)) - Remove all documents after the tests has be finished
My code feels not very good to me
describe('article module', function () {
before(function () {
MongoClient.connect(mongoServer, (err, db) => {
expect(err).to.be.null
db.collection('content', (err, collection) => {
expect(err).to.be.null
collection.findOne({ _id: articleId }, (err, item) => {
expect(err).to.be.null
if (!item) collection.insertOne(articles.main)
db.close()
})
})
})
})
after(function () {
MongoClient.connect(mongoServer, (err, db) => {
expect(err).to.be.null
db.collection('content', (err, collection) => {
expect(err).to.be.null
collection.remove({ _id: articleId }, (err, removed) => {
expect(err).to.be.null
db.close()
})
})
})
})
})
connectyields a poolable connection).