title: Unable to send authenticated request in tests using Jest, Supertest, Passport, Koa2
body: <p>Despite my best attempts to correctly write test code to authenticate a request agent in Setup blocks or previous describe/it blocks, any request I make from the agent in subsequent describe/it blocks never completes as 200. </p>
  
  <p>Example code:</p>
  
  <pre><code>const request = require('supertest');
  const server = require('../server');
  
  let agent = request.agent(server);
  let fakePerson = null;
  
  beforeEach(async (done) =&gt; {
    fakePerson = await Person.createMock();
  
    agent.post(‘/login’)
         .send({
             email: ‘test@user.com’,
             password: ‘password’
          })
          .end(function(err, res) {
              if (err) throw err;
              done();
          });
  });
  
  describe('GET /users/:id', () =&gt; {
      it ('renders user profile', () =&gt; {
        return agent
          .get(`/users/${fakePerson.id}`)
          .expect(200)
      });
  });
  </code></pre>
  
  <p>I thought it might have something to do with how I was forming the async calls syntactically. But after trying different ways of returning the login call in the <code>beforeEach</code> block using <code>return</code>, <code>.end()</code> syntax, even async/await, I've determined (ie given up) that the code must be composed properly. Could it be something else?</p>
  
  <p>Referenced articles/resources:</p>
  
  <ul>
  <li><a href="https://stackoverflow.com/questions/14001183/how-to-authenticate-supertest-requests-with-passport">How to authenticate Supertest requests with Passport?</a></li>
  <li><a href="https://medium.com/@juha.a.hytonen/testing-authenticated-requests-with-supertest-325ccf47c2bb" rel="nofollow noreferrer">https://medium.com/@juha.a.hytonen/testing-authenticated-requests-with-supertest-325ccf47c2bb</a></li>
  <li><a href="https://gist.github.com/joaoneto/5152248" rel="nofollow noreferrer">https://gist.github.com/joaoneto/5152248</a></li>
  <li><a href="https://medium.com/@bill_broughton/testing-with-authenticated-routes-in-express-6fa9c4c335ca" rel="nofollow noreferrer">https://medium.com/@bill_broughton/testing-with-authenticated-routes-in-express-6fa9c4c335ca</a></li>
  <li><a href="https://github.com/visionmedia/supertest/issues/46" rel="nofollow noreferrer">https://github.com/visionmedia/supertest/issues/46</a></li>
  </ul>
  
  <p>Package versions:</p>
  
  <ul>
  <li>"koa": "^2.4.1"</li>
  <li>"koa-passport": "^4.0.1"</li>
  <li>"passport-json": "^1.2.0"</li>
  <li>"passport-local": "^1.0.0"</li>
  <li>"supertest": "^3.0.0"</li>
  <li>"jest": "^22.1.3"</li>
  </ul>