title: Supertest and jest: cannot close express connection
body: <p>I am using jest and supertest to raise instance of express and run tests on it.</p>
  <p>I faced with the problem of busy port that cannot still solve.</p>
  <p>In my test I do the next:</p>
  <pre><code>import supertest from 'supertest';
  const agent = supertest(app);
  </code></pre>
  <p>Then I go requests with agent and everything works fine.
  Until I am running another test.
  In app.js I have:</p>
  <pre><code>var app = express();
  
  app.post('/auth/changePassword',VerifyToken, auth.changePassword);
  
  app.listen(4001, function () {
      console.log('Server is running');
  });
  </code></pre>
  <p>So first spec runs perfect. But second tries to listen to the port that is already in use.
  I really do not know how to close the connection here.</p>
  <p>I tried <code>app.close()</code> but got error that such method. This is clear, that I have to assign</p>
  <pre><code>server = app.listen(4001, function () {
      console.log('Server is running');
  });
  server.close();
  </code></pre>
  <p>But do not know how can I do that.
  I also tried to preset agent in jest.setup and assign it to global variable</p>
  <pre><code>import app from &quot;../../server/app&quot;;
  import supertest from 'supertest';
  
  const agent = supertest(app);
  global.agent = agent;
  </code></pre>
  <p>But situation is the same, first test passed, second tries to raise express on the same port.</p>