Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to run two describe blocks sequentially in a mocha-chai test suite

In one of my mocha-chai test, I have two describe blocks. In each describe block I have minimum two 'it' blocks. The second describe blocks repeats similar things what the first describe block does and something extra. When I am giving a run I feel that both the describe blocks start simultaneously one after another causing the test case fail. If I run them individually by commenting out one of the describe blocks they run fine.

Please notice that every time I am clearing the database and giving a run from the scratch so that each and every API that I test is self contained and not dependent on results of other describe blocks.

How can I sequence them so that the second describe blocks runs after the first and so on if I have more than two describe blocks.

Below is my code :

var server;
var mongodb;

before(function (done) {
    server = require('../../../app.js'); // same as "node app.js"
    done();
})

after(function (done) {
    server.close();
})

beforeEach(function (done){
    clear_the_db();
    done();
})

var json_obj = {"a":"b"};
function clear_the_db() {
    var mongoObj = mongoose.model('modelname');
    mongoObj.remove({}, function(err){
    if(!err) {
        console.log('MongoDb collections removed');
    } else {
        console.log('Error is= '+err);
    }
})

describe('First:POST call to insert data into project', ()=> {
    clear_the_db();
    it('First:Creating project', (done) => {
        chai.request(server)
        .post('/create/myproject')
        .send()
        .end((err, res) => {
            expect(res.statusCode).to.equal(200);
            done();
        });
    });

    it('First:Inserting data into created project', (done) => {
        chai.request(server)
        .post('/data/myproject')
        .send(json_obj)
        .end((err, res) => {
            expect(res.statusCode).to.equal(200);
            done();
        });
    });

});
describe('Second:POST call to insert data into project', ()=> {
    clear_the_db();
    it('Second:Creating project', (done) => {
        chai.request(server)
        .post('/create/myproject')
        .send()
        .end((err, res) => {
            expect(res.statusCode).to.equal(200);
            done();
        });
    });

    it('Second:Inserting data into created project', (done) => {
        chai.request(server)
        .post('/data/myproject')
        .send(json_obj)
        .end((err, res) => {
            expect(res.statusCode).to.equal(200);
            done();
        });
    });

    it('Second:Fetching data from the created project', (done) => {
        chai.request(server)
        .get('/data/myproject')     
        .end((err, res) => {
            expect(res.statusCode).to.equal(200);
            done();
        });
    });
});

Updated code after reading hooks from https://mochajs.org/:

var server;
var mongodb;

before(function (done) {
    server = require('../../../app.js'); // same as "node app.js"
    done();
})

after(function (done) {
    server.close();
})


var json_obj = {"a":"b"};
function clear_the_db() {
    var mongoObj = mongoose.model('modelname');
    mongoObj.remove({}, function(err){
        if(!err) {
            console.log('MongoDb collections removed');
        } else {
            console.log('Error is= '+err);
        }
    })
}

describe("This is outer-most describe", function() { 
    beforeEach(function (done){
        clear_the_db();
    })

    describe('First:POST call to insert data into project', ()=> {
        it('First:Creating project', (done) => {
            chai.request(server)
            .post('/create/myproject')
            .send()
            .end((err, res) => {
                 expect(res.statusCode).to.equal(200);
                 chai.request(server)
                .post('/data/myproject')
                .send(json_obj)
                .end((err, res) => {
                    expect(res.statusCode).to.equal(200);
                });
                done();
            });
        });         
    });

    describe('Second:POST call to insert data into project', ()=> {
        it('Second:Creating project', (done) => {
            chai.request(server)
            .post('/create/myproject')
            .send()
            .end((err, res) => {
                expect(res.statusCode).to.equal(200);
                chai.request(server)
                .post('/data/myproject')
                .send(json_obj)
                .end((err, res) => {
                     expect(res.statusCode).to.equal(200);
                     chai.request(server)
                     .get('/data/myproject')        
                     .end((err, res) => {
                         expect(res.statusCode).to.equal(200);
                     });
                });
                done();
            });
        });         
    });
}); 
like image 621
A.G.Progm.Enthusiast Avatar asked Dec 09 '25 12:12

A.G.Progm.Enthusiast


1 Answers

Mocha tests run sequantially by default. You must look for the problem elsewhere.

Possible reasons:

  1. Both of your suites are with the same name -POST call to insert data into project.
  2. If clear_the_db(); is asynchronous, it is not guaranteed to have finished executing before running the it() blocks. Instead you should do the cleanup in a beforeEach hook callback and continue to the next test case when the task is finished. Example:

    function clear_the_db(doneCb) {
      var mongoObj = mongoose.model('modelname');
    
      mongoObj.remove({}, function(err){
        if(!err) {
          console.log('MongoDb collections removed');
        } else {
          console.log('Error is= '+err);
        }
        // call the Mocha done() callback function 
        doneCb();
      });
    }
    
    // in your beforeEach hook, pass 'done' to 'clear_the_db'
    beforeEach(function (done){
      clear_the_db(done);
    });
    
like image 156
Tsvetan Ganev Avatar answered Dec 12 '25 05:12

Tsvetan Ganev



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!