1

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:

  1. Remove all documents { _id: articleId } at the beginning (right now missing in the code)
  2. Insert new document to DB (collection.insertOne(articles.main))
  3. 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()
        })
      })
    })
  })
})
5
  • Just put it into a function and call that function........................... Commented Aug 14, 2017 at 20:14
  • You can actually chain this using promises, it'll make it cleaner Commented Aug 15, 2017 at 7:43
  • @AvinduHewa Could you post a code example? Commented Aug 15, 2017 at 9:13
  • I don't really see what's wrong with what you have, I'd say it's cleaner to open a connection when needed as opposed to keeping an active connection opened for the full spec (unless connect yields a poolable connection). Commented Aug 15, 2017 at 10:20
  • @James Ok, thanks for that. I was thinking connecting once would be better... So I will keep everything. Commented Aug 15, 2017 at 10:23

1 Answer 1

1
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 the db using a third party promise as well

Sign up to request clarification or add additional context in comments.

1 Comment

I hope it helped , but i dont understand the downvote

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.