title: `npm test` never exits
body: <p>I use <a href="https://mochajs.org/" rel="noreferrer">mocha</a> to run my unit tests, and often use <code>npm test</code> in order to run it. My <code>package.json</code> contains these script definitions:</p>
  
  <pre><code>"pretest": "NODE_ENV=test node migrate all",
  "test": "DEBUG= NODE_ENV=test mocha --recursive",
  </code></pre>
  
  <p>If I run either one of these commands directly in my shell (i.e. not going through <code>npm</code>) they execute fine (790 tests takes about 2m to run, the migration script is done in under 1s). The processes also exit cleanly.</p>
  
  <p>If, however, I run these via <code>npm test</code>, everything runs in exactly the same way but the process doesn't exit (I have to manually cancel it with <code>^c</code>).</p>
  
  <p>I can't work out how to debug exactly what is going on here in order to work out why the process isn't exiting.</p>
  
  <p>It's worth noting that if I test a subdirectory (<code>npm test ./test/queue</code>) which does not interact <em>at all</em> with the database, then the process exits fine. These tests do, however, interact with an AMQP broker so there is some activity over sockets. This suggests to me that the database connection is causing the problem. I am using <a href="http://knexjs.org" rel="noreferrer">knex</a> to connect to a <code>postgres9.6</code> server. This also suggests that the <code>pretest</code> script is not the problem. If I try to run a suite of tests which <em>do</em> interact with the database, the process never exits (so presumably the open sockets are preventing it from doing so, but why this should only happen in the case of <code>npm test</code> rather than direct execution is beyond me).</p>
  
  <p>Extra info:</p>
  
  <ul>
  <li>node version: 8.7.0</li>
  <li>mocha version: 4.0.1</li>
  <li>npm version: 5.6.0</li>
  <li>knex version: 0.14.0</li>
  <li>I am using <code>async</code>/<code>await</code> in my codebase</li>
  </ul>