title: Jest tests hang due to open Sequelize connections
body: <h1>The Setup</h1>
  
  <p>I have a NodeJS <a href="https://github.com/TechAndCheck/tech-and-check-alerts/" rel="noreferrer">project</a> that uses:</p>
  
  <ul>
  <li><a href="https://jestjs.io/" rel="noreferrer">Jest</a> for testing.</li>
  <li><a href="https://sequelize.org/" rel="noreferrer">Sequelize</a> as an ORM.</li>
  </ul>
  
  <p>Sequelize is instantiated when loading the models module which gets imported by a few of the files that are being tested with Jest.</p>
  
  <h1>The Problem</h1>
  
  <p>Jest tests pass but then hang with the message:</p>
  
  <blockquote>
    <p>Jest did not exit one second after the test run has completed.</p>
    
    <p>This usually means that there are asynchronous operations that weren't
    stopped in your tests. Consider running Jest with
    <code>--detectOpenHandles</code> to troubleshoot this issue.</p>
  </blockquote>
  
  <p><strong><em>Note:</strong> Adding <code>--detectOpenHandles</code> to the test call does not affect the output.</em></p>
  
  <p>I do not actually invoke the <code>sequelize</code> object from any test paths, however some of the tested files import the <code>models</code> module and therefore Sequelize is instantiated.</p>
  
  <p>(I will also note that this only occurs on my TravisCI environment, but I suspect this is a red herring.)</p>
  
  <h1>The Context</h1>
  
  <p>Because Jest is running tests in parallel, the <code>models</code> module is loaded multiple times during the entire test process.  I confirmed this with debug output saying <code>SEQUELIZE LOADED</code> which appears multiple times when I run the tests.</p>
  
  <h1>The Attempts</h1>
  
  <ol>
  <li><p>I did attempt to invoke <code>sequelize.close()</code> inside of a <a href="https://github.com/TechAndCheck/tech-and-check-alerts/blob/de9cdd89d2a92fcafabb3a8c4568b0ea835bbc86/src/test/globalTeardown.js" rel="noreferrer"><code>globalTeardown</code></a> but this appears to simply open (and then close) a new sequelize connection.</p></li>
  <li><p>Since none of the tests actually rely on a database connection, I attempted running <code>sequelize.close()</code> within the <code>models</code> module immediately before export.  This fixed the issue (though obviously is not a solution).</p></li>
  <li><p>I have attempted to configure the test connection pools to aggressively end connections.</p></li>
  </ol>
  
  <pre><code>const sequelizeConfig = {
    ...
    pool: {
        idle: 0,
        evict: 0,
      }
  }
  </code></pre>
  
  <p>This did nothing.</p>
  
  <h1>The Requirements</h1>
  
  <ol>
  <li><p>I don't want to use a brute force solution such as running <code>--forceExit</code> via Jest when I run my tests. This feels like it is ignoring the root issue and might expose me to other kinds of mistakes down the line.</p></li>
  <li><p>My tests are spread across dozens of files, which means needing to invoke something in an <code>afterAllTests</code> would require a lot of redundancy and would introduce a code smell. </p></li>
  </ol>
  
  <h1>The Question</h1>
  
  <p>How can I ensure that sequelize connections are closed after tests finish, so they don't cause Jest to hang?</p>