lundi 28 mars 2016

Mocha js before hook and require not executing properly

I am writing unit tests for a piece for a project, the database module is separate (it sets up connection to the database and has bulk insertion method). The code in my unit test file is as follows:

var database = require('./db.js');  //once the database is connected there is a log saying connected to the database 
var Data = database.model;  //module export for the model 

before(function(){

    console.log("We are in the before hook");
    for(var i = 1; i <= 10; i++){

      var startDate = new Date(2016,1,1,0,0,0,0);
      var endDate = new Date(2016,1,1,1,0,0,0);

      var data = test_data.genIncreasing('Watch_'+i , startDate.getTime(), endDate.getTime() , 2000, 9); //will get around 3600 points
      console.log('Inserting ' + data.length + ' datapoints into database');  
      database.bulkInsert(data, function(err, data){
          if(err){
            Should.fail('Could not insert data into data base');
          }else{
            console.log('Inserted Watch_' + i + ' data into database');
          }
      });
     }
});

Now in my console I am expecting to see

Mongoose default connection open to DB-URI

followed by

We are in the before hook

Inserting 1800 datapoints into database

Inserting 1800 datapoints into database [10 times]

But I get

We are in the before hook

Inserting 1800 datapoints into database

Inserting 1800 datapoints into database [10 times]

Followed by

Mongoose default connection open to: DB_URI

I did a bit of searching and found out requires are suppose to be synchronous and so are mocha unit tests. What am I missing here? Can you give me an insight into what is happening?

Aucun commentaire:

Enregistrer un commentaire