title: How to correctly close express server between tests using mocha and chai
body: <p>I am looking for a correct way to completely reset my express server between tests! It seems that this is not just a problem for me, many other users have asked the same question,
  and many blog posts had been written on the argument. The proposed solutions are not working nor satisfactory for me. Here there is another similar question and an article that well describes the problem and suggests some solutions:</p>
  
  <p>Stack overflow
  <a href="https://stackoverflow.com/questions/23230217/closing-an-express-server-after-running-jasmine-specs/41002393#41002393">Closing an express server after running jasmine specs</a></p>
  
  <p>Blog:
  <a href="https://glebbahmutov.com/blog/how-to-correctly-unit-test-express-server/" rel="noreferrer">https://glebbahmutov.com/blog/how-to-correctly-unit-test-express-server/</a></p>
  
  <p>Here a package is specifically created to solve this problem:
  <a href="https://www.npmjs.com/package/server-destroy" rel="noreferrer">https://www.npmjs.com/package/server-destroy</a></p>
  
  <p>Now, a minimal working example to reproduce my condition.
  In the code under test, an express server is created; when called on some endpoint, the server increments a value and returns it:</p>
  
  <pre><code>( function() {
     'use strict'
  
     const enableDestroy = require( 'server-destroy' )
     const app = require( 'express' )()
     const http = require( 'http' )
  
     let val = 0
  
     app.use( '/inc', (req, res) =&gt; {
        val ++
        res.send(val.toString())
     } )
  
     const server = http.createServer( app )
  
     server.listen( 3000 )
  
     enableDestroy(server);
  
     module.exports = server
  
  } )()
  </code></pre>
  
  <p>The test consists in two identical test cases; both of them call the server on the endpoint, and check the returned value.
  <code>before_each</code> and <code>after_each</code> sections are provided in order to assure a new connection is created before the single test case is run, and then closed, to assure independence between the two test cases:</p>
  
  <pre><code>const chai = require( 'chai' )
  const chaiHttp = require( 'chai-http' )
  const expect = chai.expect
  chai.use( chaiHttp )
  
  let server
  
  describe( 'first test group', () =&gt; {
     beforeEach( () =&gt; {
        server = require( './server' )
     } ),
  
     afterEach( ( done ) =&gt; {
        server.destroy( done )
        delete require.cache[require.resolve( './server' )]
     } ),
  
     it( 'should respond 1', ( done ) =&gt; {
        chai.request( server )
           .get( '/inc' )
           .set( 'Connection', 'close' )
           .end( ( err, res ) =&gt; {
              expect( res.text ).to.be.equal( '1' )
              done()
           } )
     } ),
  
     it( 'should respond 1', ( done ) =&gt; {
        chai.request( server )
           .get( '/inc' )
           .set( 'Connection', 'close' )
           .end( ( err, res ) =&gt; {
              expect( res.text ).to.be.equal( '1' )
              done()
           } )
     } )
  } )
  </code></pre>
  
  <p>The test fails because the server is <em>not running</em> after the first test. Please note that in the <code>after_each</code> section I forced a cache cleaning for lost completely the last server instance. The test succeeds if a single test case is run:</p>
  
  <pre><code>first test group
      ✓ should respond 1
      1) "after each" hook for "should respond 1"
  
  
    1 passing (70ms)
    1 failing
  
    1) first test group
         "after each" hook for "should respond 1":
       Error: Not running
        at Server.close (net.js:1620:12)
        at emitCloseNT (net.js:1671:8)
        at _combinedTickCallback (internal/process/next_tick.js:135:11)
        at process._tickCallback (internal/process/next_tick.js:180:9)
  </code></pre>
  
  <p>The configuration I used:</p>
  
  <ul>
  <li>Node 8.11.1</li>
  <li>Express: "4.16.3"</li>
  <li>Mocha 5.0.5</li>
  <li>chai 4.1.2</li>
  <li>chai-http 4.0.0</li>
  </ul>
  
  <p>How can I solve this problem? And what does the error message mean?</p>