title: how do I close pg-promise connection in node.js after all tests have run in jest?
body: <p>I need to close a PG-Promise database connection after testing a function in Jest.</p>
  
  <p>This is initialised in one place(<em>db.js</em>) and <code>require</code>d everywhere it is needed. In the case of the code below, it is required by <em>seed.js</em> which <em>seed.spec.js</em> is testing.</p>
  
  <p>I know there is an <code>afterAll</code> hook in Jest, but that will close the connection everywhere which might cause tests to fail incorrectly?</p>
  
  <p>The problem <em>is</em> solved with the <code>--forceExit</code> option, but it gives an error message and doesn't feel like the right way to solve this?</p>
  
  <p>db.js:</p>
  
  <pre><code>const pgp = require('pg-promise')();
  const db = pgp(connection);
  
  module.exports = {db, pgp};
  </code></pre>
  
  <p>seed.spec.js:</p>
  
  <pre><code>require('dotenv').config();
  const {pgp} = require('./db');
  
  expect.extend(require('jest-json-schema').matchers);
  
  const schema = require('./schemas');
  const generate = require('./generate');
  const seed = require('./seed');
  const data = generate();
  
  test ('the data returned by the seed function matches the schema', () =&gt; {
    return seed(data)
      .then(outputData =&gt; {
        expect(outputData).toMatch(schema);
      });
  });
  </code></pre>
  
  <p>P.S. I have seen similar questions, but none of them quite matches my situation.</p>