title: NodeJS: Error found: listen EADDRINUSE :::3000?
body: <br/>
  I'm currently facing a bug that I can't resolve and I have been struggling for some hours.<br/>
  I'm using the following versions:<br/>
  Node: 8.11.3<br/>
  Express: 4.16.3<br/>
  Jest: 22.2.2<br/>
  Mongoose: 5.2.3<br/>
  <br/>
  I'm trying to do some integration tests with jest and I have 2 files with tests.<br/>
  In each file I wrote the following:<br/>
  <pre><code>// Create the server before each test.
  beforeEach(() =&gt; {
      server = require('../../index');
  });
  
  // Close the server after each test.
  afterEach(async () =&gt; {
      if (server) {
          server.close();
      }
  });
  </code></pre>
  <p>In <code>index.js</code> I have the following (This is not all the code, but the relevant code for the bug):<br /></p>
  <pre><code>// Listen to the server.
  const port = config.PORT || process.env.PORT || 3000;
  module.exports = app.listen(port, () =&gt; {
     winston.info(`Listening to port ${port}...`);
  });
  </code></pre>
  <p>When I run <strong><code>npm test</code></strong>
  I get this exception all the time:</p>
  <pre><code>**listen EADDRINUSE :::3000**
  
    10 | // Listen to the server
    11 | const port = config.PORT || process.env.PORT || 3000;
  &gt; 12 | module.exports = app.listen(port, () =&gt; {
    13 |     winston.info(`Listening to port ${port}...`);
    14 | });
    15 |
  </code></pre>
  <p>I tried several ways to solve this by adding <code>async await</code> to <code>beforeEach</code> and for <code>afterEach</code> and tried also to put the sever.close in <code>afterAll</code> and in <code>beforeAll</code> but still got the same error.<br/><br/>
  Then, I tried to solve by doing this:<br/>
  <a href="https://stackoverflow.com/questions/43003870/how-do-i-shut-down-my-express-server-gracefully-when-its-process-is-killed">How Do I Shut Down My Express Server Gracefully When Its Process Is Killed?</a><br/><br/>
  But again, with no luck.<br/>
  Finally, when I wrote all the tests in 1 file, it works without this <code>error</code>. Does anybody know how to solve this? I don't want to write all my integration tests in 1 file..<br />
  Thanks!</p>