javascript - How do I check when multiple chai-http requests are really done in a mocha before block? - Stack Overflow

I'm using a before block in a set of mocha unit tests and within them I'm iterating over a se

I'm using a before block in a set of mocha unit tests and within them I'm iterating over a set of calls to get information from a REST API. I'm using chai-http to do this. However I am running into the problem that the done() method is being called before the series of n requests I make have pleted. Calling done in the end block results in multiple done() calls but putting outside of the block means it's called before I'm really done! Here is an example of before block:

var flags = [];
var groups = [];

// This functions correctly 1 done() called at the end
before(function(done) {
    chai.request(server)
        .get('/groups')
        .end(function(err, res){
             groups = JSON.parse(res.text);
             done();
        });
    });

before(function(done) {
    groups.forEach(function(rec) {
        chai.request(server)
            .get('/groups/' + rec.KEYWORD_GROUP_ID + '/groupflags')
            .end(function(res, err) {
                Array.prototype.push.apply(flags, JSON.parse(res.text));
                // A done() here gets called n times
                });
        // But here it's called before the requests all end
        done();
        });

Is there a way of detecting when all of these requests have pleted then I can call a single done() to ensure my tests are only executed with the correct context set up?

I'm using a before block in a set of mocha unit tests and within them I'm iterating over a set of calls to get information from a REST API. I'm using chai-http to do this. However I am running into the problem that the done() method is being called before the series of n requests I make have pleted. Calling done in the end block results in multiple done() calls but putting outside of the block means it's called before I'm really done! Here is an example of before block:

var flags = [];
var groups = [];

// This functions correctly 1 done() called at the end
before(function(done) {
    chai.request(server)
        .get('/groups')
        .end(function(err, res){
             groups = JSON.parse(res.text);
             done();
        });
    });

before(function(done) {
    groups.forEach(function(rec) {
        chai.request(server)
            .get('/groups/' + rec.KEYWORD_GROUP_ID + '/groupflags')
            .end(function(res, err) {
                Array.prototype.push.apply(flags, JSON.parse(res.text));
                // A done() here gets called n times
                });
        // But here it's called before the requests all end
        done();
        });

Is there a way of detecting when all of these requests have pleted then I can call a single done() to ensure my tests are only executed with the correct context set up?

Share Improve this question asked Apr 21, 2017 at 3:31 JSDevGuyJSDevGuy 1392 silver badges11 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 3

You could try with async.whilst(). Count up a counter to groups.length and then hit done() in the callback. Link to the function documentation: (http://caolan.github.io/async/docs.html#whilst)

Something like...

let counter = 0;
async.whilst(
    () => {
        // Test if we have processed all records
        return counter < groups.length;
    },
    (callback) => {
        let rec = groups[counter++]; // Sorry Douglas
        chai.request(server)
            .get('/groups/' + rec.KEYWORD_GROUP_ID + '/groupflags')
            .end(function (res, err) {
                Array.prototype.push.apply(flags, JSON.parse(res.text));
                callback(null, counter);
            });
    },
    (err) => {
        assert(!err, err);
        done();
    }
);

As Alex requested here is what I had initially as a solution:

before('delete keywords in a group', function(done) {
    var count = 0;
    var length = groups.length;

    if (length === 0) {done();}

    groups.forEach(function (rec) {
        chai.request(server)
            .delete('/keywords/' + rec.id)
            .end(function (err, res) {
                if (err) {
                    console.error('Delete keywords err: ' + err.message);
                    this.skip();
                } else {
                    count++;
                    if (count === length) {done();}
                    }
            });
        });
    });

This seems to be working but I think for any more plex cases (for example a cascade style delete) the async library provides a more elegant and reliable solution. Hence it is a better fit for the general case.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745602928a4635507.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信