visionmedia/mocha — Issue #268: how to make mocha wait for db connection?
Author: nagyv
Created: 2012-02-17 01:13:37+00:00
State: open

I would like to write a beforeEach handler that purges the database, like

beforeEach(function(done){
mainapp.statusapp.Users._withCollection(function(err, collection){
if(err) done(err);
collection.remove({}, done);
})
});

I connect to my server asyncronouosly at the beginning of server.js

var db = new Db({
name: 'test',
host: '127.0.0.1',
port: 27017
});
db.on('database', function(status){
var error = status == 'open' ? null : status;
if (error) throw 'Could not connect to database';
else {
statusapp.Users.fetch({
error: function(err) {
throw 'Could not fetch users';
},
success: function(results) {
console.log('Initial fetch success');
}
});
}
})

unfortunately, the tests don't wait for this connection to happen, and putting the test inside a `db.on` handler did not solve the issue as simply my tests were not recognised at all, and the script finished

how can I make mocha (actually in general javascript code) to wait to start some task?