javascript - NodeJS/MongoDB: Only connect one time to DB -


i'm doing e2e tests of nodejs application. in before/after hooks need add/remove mongodb documents , how that:

shouldn't possible connect 1 time mongo server @ all?

what do:

  1. remove documents { _id: articleid } @ beginning (right missing in code)
  2. insert new document db (collection.insertone(articles.main))
  3. remove documents after tests has finished

my code feels not 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()         })       })     })   }) }) 

describe('article module', () => {   before(() => {      mongoclient.connect(mongoserver, (err, db) => {       expect(err).to.be.null        const content = db.collection('content');        content.findone({ _id: articleid })       .then(item => {         return content.insertone(articles.main);       })       .then(insertresult => {         db.close();       })       .catch(err => {         expect(err).to.be.null;       })     })    })    after(() => {     mongoclient.connect(mongoserver, (err, db) => {       expect(err).to.be.null        const content = db.collection('content');        content.remove({ _id: articleid })       .then(removed => {         db.close();       })       .catch(err => {         expect(err).to.be.null;       })     })   }) }) 

you can call db using third party promise well


Comments

Popular posts from this blog

python Tkinter Capturing keyboard events save as one single string -

android - InAppBilling registering BroadcastReceiver in AndroidManifest -

javascript - Z-index in d3.js -