最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

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

programmeradmin1浏览0评论

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 menting 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 /:

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();
            });
        });         
    });
}); 

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 menting 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/:

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();
            });
        });         
    });
}); 
Share Improve this question edited Nov 12, 2018 at 10:35 Byrne Reese 2691 silver badge7 bronze badges asked Jan 9, 2018 at 10:11 A.G.Progm.EnthusiastA.G.Progm.Enthusiast 1,0061 gold badge15 silver badges39 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 3

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);
    });
    

Using Visual Studio 2017, I was having similar problems when I had multiple describes on the same js test file. On the same set of tests, they would sometime fail depending if I ran all the test under the project, ran all of the tests under debug, just that files test, or just one test. I tried various different methods of getting around this situation. I was about to give up, but then decided to run it outside of Visual Studio and run it from the mand line. Voilà!!! It worked!!!

发布评论

评论列表(0)

  1. 暂无评论