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

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

Posted by: admin November 29, 2017 Leave a comment

Questions:

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

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.

Questions:
Answers:

I implemented it a bit different.

  1. I removed all documents in the “before” hook – found it a lot faster than dropDatabase().
  2. I used Promise.all() to make sure all documents were removed before exiting the hook.

    beforeEach(function (done) {
    
        function clearDB() {
            var promises = [
                Model1.remove().exec(),
                Model2.remove().exec(),
                Model3.remove().exec()
            ];
    
            Promise.all(promises)
                .then(function () {
                    done();
                })
        }
    
        if (mongoose.connection.readyState === 0) {
            mongoose.connect(config.dbUrl, function (err) {
                if (err) {
                    throw err;
                }
                return clearDB();
            });
        } else {
            return clearDB();
        }
    });