admin管理员组文章数量:1433095
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?
2 Answers
Reset to default 3You 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.
本文标签:
版权声明:本文标题:javascript - How do I check when multiple chai-http requests are really done in a mocha before block? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745602927a2665672.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论