I am testing an express API with supertest.
I couldn't get multiple requests in a test case to work with supertest. Below is what i tried in a test case. But the test case seem to only execute the last call which is the HTTP GET.
it('should respond to GET with added items', function(done) {
var agent = request(app);
agent.post('/player').type('json').send({name:"Messi"});
agent.post('/player').type('json').send({name:"Maradona"});
agent.get('/player').set("Accept", "application/json")
.expect(200)
.end(function(err, res) {
res.body.should.have.property('items').with.lengthOf(2);
done();
});
);
Anything i am missing here, or is there another way to chain http calls with superagent?
I built upon Tim’s reply but used
async.waterfall
instead, to be able to do assert tests on the results (note: I use Tape here instead of Mocha):This can be most elegantly solved with promises, and there's a really useful library to use promises with supertest: https://www.npmjs.com/package/supertest-as-promised
Their example:
Tried to put this in a comment above, formatting wasn't working out.
I'm using async, which is really standard and works really well.
The calls are made asynchronous, so you need to use callback functions to chain them.