Async function in mocha before() is alway finished before it() spec?

JavascriptAsynchronousmocha.js

Javascript Problem Overview


I have a callback function in before() which is for cleaning database. Is everything in before() guaranteed to finish before it() starts?

before(function(){
   db.collection('user').remove({}, function(res){}); // is it guaranteed to finish before it()? 
});

it('test spec', function(done){
  // do the test
});

after(function(){
});

Javascript Solutions


Solution 1 - Javascript

For new mocha versions :

You can now return a promise to mocha, and mocha will wait for it to complete before proceeding. For example, the following test will pass :

let a = 0;
before(() => {
  return new Promise((resolve) => {
    setTimeout(() => {
      a = 1;
      resolve();
    }, 200);
  });
});
it('a should be set to 1', () => {
  assert(a === 1);
});

You can find the documentation here

For older mocha versions :

If you want your asynchronous request to be completed before everything else happens, you need to use the done parameter in your before request, and call it in the callback.

Mocha will then wait until done is called to start processing the following blocks.

before(function (done) {
   db.collection('user').remove({}, function (res) { done(); }); // It is now guaranteed to finish before 'it' starts.
})

it('test spec', function (done) {
  // execute test
});

after(function() {});

You should be careful though, as not stubbing the database for unit testing may strongly slow the execution, as requests in a database may be pretty long compared to simple code execution.

For more information, see the Mocha documentation.

Solution 2 - Javascript

Hopefully your db.collection() should return a promise. If yes then you can also use async keyword in before()

// Note: I am using Mocha 5.2.0.
before(async function(){
   await db.collection('user').remove({}, function(res){}); // it is now guaranteed to finish before it()
});

Solution 3 - Javascript

you can use an IIFE (Immediately Invoked Function Expression).

Here is an example:

before(function () {
    (async () => {
        try {
            await mongoose.connect(mongo.url, { useNewUrlParser: true, useUnifiedTopology: true });
        } catch (err) {
            console.log(err);
        }
    })();
});

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionNicolas S.XuView Question on Stackoverflow
Solution 1 - JavascriptClément BerthouView Answer on Stackoverflow
Solution 2 - JavascriptMandeep SinghView Answer on Stackoverflow
Solution 3 - JavascriptYarden PoratView Answer on Stackoverflow