title: Memory leak with jest (node, koa, sequelize)
body: <p>Stack:
  <code>
    System:
      OS: macOS High Sierra 10.13.4
      CPU: x64 Intel(R) Core(TM) i7-4770HQ CPU @ 2.20GHz
    Binaries:
      Node: 8.1.4 - ~/.nvm/versions/node/v8.1.4/bin/node
      npm: 6.0.0 - ~/.nvm/versions/node/v8.1.4/bin/npm
    npmPackages:
      jest: ^23.0.1 =&gt; 23.0.1
      sequelize: ^4.37.10
      pg: ^7.4.3
      koa: ^2.3.0
      babel-jest: ^22.4.3
  </code></p>
  
  <h2>1. Preface</h2>
  
  <p>We recently switched all our API's test from Mocha to Jest.
  We have around 90 tests, half of them require to run synchronously due to them using our testing database (running seeds between them), so we have to use --runInBand.  </p>
  
  <p>I unfortunately can't share my code as it is private.  </p>
  
  <h2>2. The problem</h2>
  
  <p>Running tests one by one was fine, I then tried to run all of them at once and things went bad.
  With the --logHeapUsage, it seems context memory isn't GC'd resulting in a Javascript heap out of memory.</p>
  
  <p>I tried using the new option --detectOpenHandles to see what could prevent the GC to work but this is what came out:
  <code>
    ●  PROMISE
            at Promise.catch (&lt;anonymous&gt;)
        at node_modules/core-js/library/modules/es6.promise.js:244:30
        at Object.&lt;anonymous&gt;.module.exports (node_modules/core-js/library/modules/_iter-detect.js:19:5)
        at Object.&lt;anonymous&gt; (node_modules/core-js/library/modules/es6.promise.js:243:74)
        at Object.&lt;anonymous&gt; (node_modules/core-js/library/fn/promise.js:4:1)
  </code>
  I have around 6-8 of these, and no clue whatsoever of where to look.</p>
  
  <p>I searched around and found out it was most likely the database connection so I added these as a global teardown:</p>
  
  <pre><code>afterAll(async () =&gt; {
      await db.close(); // Sequelize instance
      server.close(); // Koa server instance used with supertest (when needed)
  });
  </code></pre>
  
  <p>This didn't change much, memory still goes up very quickly (30-40 MB per test).
  In the end, I wrote a small file launching jest multiple times to avoid memory problems and stitching coverage report together but this isn't ideal.</p>