same query origin node.js express supertest superagent

node.js - query - superagent same origin



¿Cómo encadenar llamadas http con superagent/supertest? (4)

Construí sobre la respuesta de Tim pero usé async.waterfall en async.waterfall lugar, para poder hacer pruebas async.waterfall sobre los resultados (nota: uso Tape aquí en lugar de Mocha):

test(''Test the entire API'', function (assert) { const app = require(''../app/app''); async.waterfall([ (cb) => request(app).get(''/api/accounts'').expect(200, cb), (results, cb) => { assert.ok(results.body.length, ''Returned accounts list''); cb(null, results); }, (results, cb) => { assert.ok(results.body[0].reference, ''account #0 has reference''); cb(null, results); }, (results, cb) => request(app).get(''/api/plans'').expect(200, cb), (results, cb) => request(app).get(''/api/services'').expect(200, cb), (results, cb) => request(app).get(''/api/users'').expect(200, cb), ], (err, results) => { app.closeDatabase(); assert.end(); } ); });

Estoy probando una API express con supertest.

No pude obtener solicitudes múltiples en un caso de prueba para trabajar con supertest. Debajo está lo que probé en un caso de prueba. Pero el caso de prueba parece ejecutar solo la última llamada que es 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(); }); );

¿Algo que me falta aquí, o hay otra forma de encadenar llamadas http con superagente?


Esto se puede resolver de forma más elegante con promesas, y hay una biblioteca realmente útil para usar las promesas con supertest: https://www.npmjs.com/package/supertest-as-promised

Su ejemplo:

return request(app) .get("/user") .expect(200) .then(function (res) { return request(app) .post("/kittens") .send({ userId: res}) .expect(201); }) .then(function (res) { // ... });


Las llamadas se realizan de forma asíncrona, por lo que debe usar funciones de devolución de llamada para encadenarlas.

it(''should respond to GET with added items'', function(done) { var agent = request(app); agent.post(''/player'').type(''json'').send({name:"Messi"}).end(function(){ agent.post(''/player'').type(''json'').send({name:"Maradona"}).end(function(){ agent.get(''/player'') .set("Accept", "application/json") .expect(200) .end(function(err, res) { res.body.should.have.property(''items'').with.lengthOf(2); done(); }); }); }); });


Traté de poner esto en un comentario anterior, el formato no estaba funcionando.

Estoy usando async, que es realmente estándar y funciona muy bien.

it(''should respond to only certain methods'', function(done) { async.series([ function(cb) { request(app).get(''/'').expect(404, cb); }, function(cb) { request(app).get(''/new'').expect(200, cb); }, function(cb) { request(app).post(''/'').send({prop1: ''new''}).expect(404, cb); }, function(cb) { request(app).get(''/0'').expect(200, cb); }, function(cb) { request(app).get(''/0/edit'').expect(404, cb); }, function(cb) { request(app).put(''/0'').send({prop1: ''new value''}).expect(404, cb); }, function(cb) { request(app).delete(''/0'').expect(404, cb); }, ], done); });