title: Testing Express Endpoints with nodemon and mocha in watch mode conflict
body: <p>I have an express api I am developing and am trying to use this project to learn all the best practices for express.  That being said I want to write unit tests for my endpoints.  I have been trying to figure out how to run my development server as well as run my mocha tests in watch mode.  This issue is that since <code>mocha</code> and <code>nodemon</code> are trying to restart/run at the same time I will get varying results on my tests which kinda defeats the purpose of having it in watch mode.</p>
  
  <p>I will a variety of the following errors:</p>
  
  <pre><code>1) GET /user/:userId Status Code 200:
       Uncaught Error: connect ECONNREFUSED 127.0.0.1:3000
        at Object.exports._errnoException (util.js:1050:11)
        at exports._exceptionWithHostPort (util.js:1073:20)
        at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1097:14)
  
    1) GET /user/:userId Status Code 200:
       Uncaught Error: socket hang up
        at createHangUpError (_http_client.js:302:15)
        at Socket.socketOnEnd (_http_client.js:394:23)
        at endReadableNT (_stream_readable.js:975:12)
        at _combinedTickCallback (internal/process/next_tick.js:80:11)
        at process._tickCallback (internal/process/next_tick.js:104:9)
  </code></pre>
  
  <p>I am assuming that these errors stem from either mocha trying to initialize another instance of my application which is already running or that the tests are run before the server is given a chance to restart. (I think this because it will sometimes run successfully)</p>
  
  <p>I have to think this is a fairly common use case so I would like to know what the standard approach for doing this sort of thing is.</p>
  
  <p>scripts in package.json </p>
  
  <pre><code> "scripts": {
      "test": "mocha --compilers js:babel-register --watch",
      "start": "nodemon src/index.js --exec babel-node --presets es2015,stage-2"
    }
  </code></pre>
  
  <p>the failing <code>user/userId</code> unit test:</p>
  
  <pre><code>import http from 'http'
  import assert from 'assert'
  
  describe('GET /user/:userId', () =&gt; {
    it('Status Code 200', done =&gt; {
      http.get('http://localhost:3000/user/1', res =&gt; {
        assert.equal(200, res.statusCode)
        done()
      })
    })
  })
  </code></pre>
  
  <p>src/index.js</p>
  
  <pre><code>import express from 'express'
  import User from './controllers/user'
  
  const app = express()
  app.get('/user/:userId',new User().getUser)
  
  export default app.listen(3000)
  </code></pre>