Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 667
TaoPR Avatar asked Feb 15 '15 11:02

TaoPR


People also ask

What is a pending test Mocha?

A pending test in many test framework is test that the runner decided to not run. Sometime it's because the test is flagged to be skipped. Sometime because the test is a just a placeholder for a TODO. For Mocha, the documentation says that a pending test is a test without any callback.

How do you skip the Mocha test?

To skip multiple tests in this manner, use this.skip() in a “before all” hook: before(function() { if (/* check test environment */) { // setup code } else { this.skip(); } }); This will skip all it , beforeEach/afterEach , and describe blocks within the suite.

How do you test async in Mocha?

Writing Asynchronous Tests with Mocha. To indicate that a test is asynchronous in Mocha, you simply pass a callback as the first argument to the it() method: it('should be asynchronous', function(done) { setTimeout(function() { done(); }, 500); });


1 Answers

@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

like image 78
Tahsin Turkoz Avatar answered Oct 16 '22 11:10

Tahsin Turkoz