express - mocha and supertest.agent not working as expected -
i'm trying write tests need authenticate first. if make multiple requests in "before()" connection refused. if split between "before()" , "it()" works cannot acheive want.
code want work:
var agent = request.agent(myexpressapp), token; before(function(done) { async.series([ function(cb) { agent .post('/1/auth/login') .send({ email: 'john@smith.com', password: 'j0hn_sm1th' }) .expect(200) .end(cb); }, function(cb) { agent .get('/1/auth/authorize') .query({ response_type: 'token', client_id: 'some id', redirect_uri: 'http://ignore' }) .expect(302) .end(function(err, res) { /* console.log(arguments) = { '0': { [error: connect econnrefused] code: 'econnrefused', errno: 'econnrefused', syscall: 'connect' } }*/ if (err) return cb(err); cb(); }); } ], done); }); it('some authenticated task', function(done) { // else done(); });
code working:
var agent = request.agent(myexpressapp), token; before(function(done) { async.series([ function(cb) { agent .post('/1/auth/login') .send({ email: 'john@smith.com', password: 'j0hn_sm1th' }) .expect(200) .end(cb); }, function(cb) { cb(); } ], done); }); it('some authenticated task', function(done) { agent .get('/1/auth/authorize') .query({ response_type: 'token', client_id: 'some id', redirect_uri: 'http://ignore' }) .expect(302) .end(function(err, res) { if (err) return done(err); done(); }); });
you have encountered issue 153 superagent. annoyingly , magically, superagent looks @ arity of callback function pass it. if function declared accepting 2 arguments, superagent conforms node convention of (error, result)
, however, if callback function looks expects other number of arguments (zero in case of use async.js), invokes function callback(res)
guess tj thought nice browser, node totally breaks convention.
the exact line of code in question here
Comments
Post a Comment