How to add a delay before starting a Mocha test case?

All we need is an easy explanation of the problem, so here it is.

I’m writing a unit test for my simple Node.js application using Mocha. The application has a class which connects to a Mongo database, fetch the record, and store the formulated record as a field. Simply, the class looks like this:

SampleClass.prototype.record = []; // Store the loaded record
SampleClass.prototype.init = function(db){
    var self = this;
    self.db = mongoose.connection; // Say we already have mongoose object initialized
    self.db.once('open',function(){
        /* schema & model definitions go here */
        var DataModel = mongoose.model( /* foobar */);
        DataModel.findOne(function(err,record){
           /* error handling goes here */ 

           self.record = record; // Here we fetch & store the data
        });
    });
}

As seen from the snippet above, once the SampleClass.init() is called, the Sample.record will not instantly get populated from the database. The data is asynchronously populated once the event ‘open’ is fired. Thus, there will possibly be a delay after SampleClass.init() until the Sample.record is populated.

So it comes into a complication when I write a Mocha test like this:

var testSampleClass = new SampleClass();

describe('SampleClass init test',function(){
    testSampleClass.init('mydb');
    it('should have 1 record read from mydb',function(){
        assert.equal(testSampleClass.record.length,1);
    });
});

The assertion above will always fail because testSampleClass.record will not get populated straightaway after init. It needs a gap of time to load the data.

How can I delay the test case so it starts a few seconds or more after testSampleClass.init is called? Is it also possible to trigger the test case right after an event of my class is fired? Otherwise, this simple case will always fail which I know this is not correct at all.

How to solve :

I know you bored from this bug, So we are here to help you! Take a deep breath and look at the explanation of your problem. We have many solutions to this problem, But we recommend you to use the first method because it is tested & true method that will 100% work for you.

Method 1

@alexpods made a great suggestion. Add following to your test collection so that each test step will wait for 500 msec before running.

  beforeEach(function (done) {
    setTimeout(function(){
      done();
    }, 500);
  });

or in ES6

 beforeEach(done => setTimeout(done, 500));

Thanks @Timmerz for the suggestion

Method 2

Use before() or beforeEach hooks (see here and here). They take done callback as argument, which you must call when some asynchronous staff will be completed. So you test should looks like:

describe('SampleClass init test',function(){
    before(function(done) {
        testSampleClass.init('mydb', done);
    });
    it('should have 1 record read from mydb',function(){
        assert.equal(testSampleClass.record.length,1);
    });
});

And your init method:

SampleClass.prototype.record = []; // Store the loaded record
SampleClass.prototype.init = function(db, callback){
    var self = this;
    self.db = mongoose.connection; // Say we already have mongoose object initialized
    self.db.once('open',function(){
        /* schema & model definitions go here */
        var DataModel = mongoose.model( /* foobar */);
        DataModel.findOne(function(err,record){
            /* error handling goes here */

            self.record = record; // Here we fetch & store the data
            callback();
        });
    });
}

Note: Use and implement method 1 because this method fully tested our system.
Thank you 🙂

All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply