mocha testing, chaining supertest requests
<p>so I am trying to test a route in my express app, and in order to do so I need to login a <code>user</code> before making my call. I create and save a <code>user</code> in the <code>beforeEach</code> function. Here is the test I am writing: </p>

<pre><code>it('should update username', function(done){
    var _this = this;
     req.post('/login')
      .send(_this.data)
      .then(function(res){
        req.put('/users/' + res.body.user._id)
          .send({ username: 'robert'})
          .expect(200)
          .end(function(err, res){
            if(err) return done(err);
            console.log(res.body);
            res.body.success.should.equal(true);
            res.body.user.username.should.match(/robert/);
            done();
        });
    });
 });
</code></pre>

<p>Here is the output I get when I run the test:</p>

<pre><code>  Users
    Routes
      Authenticated
POST /login 200 195.059 ms - 142
PUT /users/568a432e1daa24083fa6778a 401 2.785 ms - 21
        1) should update username
    Unauthenticated
GET /users 401 1.502 ms - 21
      ✓ should return 401


  1 passing (516ms)
  1 failing

  1) Users Routes Authenticated should update username:
     Error: expected 200 "OK", got 401 "Unauthorized"
      at Test._assertStatus (node_modules/supertest/lib/test.js:232:12)
      at Test._assertFunction (node_modules/supertest/lib/test.js:247:11)
      at Test.assert (node_modules/supertest/lib/test.js:148:18)
      at Server.assert (node_modules/supertest/lib/test.js:127:12)
      at net.js:1273:10
</code></pre>

<p>I'm confused why it's responding in a <code>401</code>, when the <code>POST /login</code> request responded with a <code>200</code>.</p>

<p>Using <code>Postman</code> I am able to create a <code>user</code>, login as that user, and with a <code>PUT</code> request I am able to update the data successfuly. So, I am assuming this has something to do with the <code>req</code> chaining of <code>supertest</code>.</p>

<p>I have written the request chaining using both <code>supertest-as-promised</code> as well as just <code>supertest</code>.</p>

<p>As far as I understand the following code behaves the same as using the <code>then()</code> syntax:</p>

<pre><code>it('should update username', function(done){
   var _this = this;
   req.post('/login')
   .send(_this.data)
   .endfunction(err, res){
      if(err) return done(err);
      req.put('/users/' + res.body.user._id)
         .send({ username: 'robert'})
         .expect(200)
         .end(function(err, res){
            if(err) return done(err);
            console.log(res.body);
            res.body.success.should.equal(true);
            res.body.user.username.should.match(/robert/);
            done();
         });
    });
});
</code></pre>

<p>I'm confused by what is going on here. Like I said, I can do this using <code>Postman</code> so I assume, this is a problem with how the request chaining is working. If you need more context I can provide more code if need be.</p>