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:
- remove documents
{ _id: articleid }
@ beginning (right missing in code) - insert new document db (
collection.insertone(articles.main)
) - 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
Post a Comment