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

@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


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();
        });
    });
}