Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

where to delete database and close connection after all tests using mocha

I'm trying to figure out where to put a function to delete database and close connection after all tests have run.

Here are my nested tests:

//db.connection.db.dropDatabase();
//db.connection.close();

describe('User', function(){
    beforeEach(function(done){
    });

    after(function(done){
    });

    describe('#save()', function(){
        beforeEach(function(done){
        });

        it('should have username property', function(done){
            user.save(function(err, user){
                done();
            });
        });

        // now try a negative test
        it('should not save if username is not present', function(done){
            user.save(function(err, user){
                done();
            });
        });
    });

    describe('#find()', function(){
        beforeEach(function(done){
            user.save(function(err, user){
                done();
            });
        });

        it('should find user by email', function(done){
            User.findOne({email: fakeUser.email}, function(err, user){
                done();
            });
        });

        it('should find user by username', function(done){
            User.findOne({username: fakeUser.username}, function(err, user){
                done();
            });
        });
    });
});

Nothing seems to work. I get Error: timeout of 2000ms exceeded

like image 948
chovy Avatar asked Dec 16 '12 05:12

chovy


1 Answers

You can define a "root" after() hook before the 1st describe() to handle cleanup:

after(function (done) {
    db.connection.db.dropDatabase(function () {
        db.connection.close(function () {
            done();
        });
    });
});

describe('User', ...);

Though, the error you're getting may be from the 3 asynchronous hooks that aren't informing Mocha to continue. These need to either call done() or skip the argument so they can be treated as synchronous:

describe('User', function(){
    beforeEach(function(done){ // arg = asynchronous
        done();
    });

    after(function(done){
        done()
    });

    describe('#save()', function(){
        beforeEach(function(){ // no arg = synchronous
        });

        // ...
    });
});

From the docs:

By adding a callback (usually named done) to it() Mocha will know that it should wait for completion.

like image 159
Jonathan Lonowski Avatar answered Sep 19 '22 15:09

Jonathan Lonowski