I would like to send multiple requests sequentially in the beore() method of my test. How do I do this without deeply nesting the code. This is what I have right now:
describe('Conf Test', function() {
this.timeout(5000);
before(function(done) {
sequelize.sync({
force: true
}).then(
function() {
request(app)
.post('/albums')
.send({name: 'Beatles'})
.set('Accept', 'application/json')
.end(function() {
request(app)
.post('/albums')
.send({name: 'Deep Purple'})
.set('Accept', 'application/json')
.end(function() {
done();
});
});
}
);
});
can't really, joys of node's callbacks. even if you could string them along it would be troublesome because then you cant use any response information for the next requests. This is a fundamental node fail really, generators will help but we'll have to add gen support to supertest at some point
Would it help if the methods returned a promise? Just like the sequelize.sync() method above.
not really, you'd still have to use .then() and a callback
Hi @visionmedia, I was able to use async to remove the deep nesting. My code is definitely more readable now. What do you think of using such utitlities with supertest?
describe('Album Test', function() {
this.timeout(5000);
before(function(done) {
async.series([
// Sync the database - sequelize.sync()
function(callback) {
domain.sequelize.sync({
force: true
}).then(function(/* models */) {
callback(null, null);
});
},
// Create an album
function(callback) {
request(app)
.post('/albums')
.send({name: 'Beatles'})
.set('Accept', 'application/json')
.end(callback);
},
// Create another album
function(callback) {
request(app)
.post('/albums')
.send({name: 'Deep Purple'})
.set('Accept', 'application/json')
.end(callback);
}
], done);
});
...
}
sounds fine to me! Use whatever works for you
what do you think of supertest-as-promised?
https://github.com/WhoopInc/supertest-as-promised
Is it any good?
Most helpful comment
Hi @visionmedia, I was able to use async to remove the deep nesting. My code is definitely more readable now. What do you think of using such utitlities with supertest?