title: Jest tests did not exit (one second) after the test run has completed using a simple Express application
body: <p>I have a simple <code>Express</code> application that exposes a RESTful API. It uses <code>Knex</code> and <code>Objection</code> to access the database, and <code>Jest</code> / <code>Supertest</code> for testing the API.</p>
  <p>I have a test that starts the server, fetches the available data from a given route and asserts the received values. Everything works fine, except that <code>Jest</code> never exits after executing this test.</p>
  <p>This is my test:</p>
  <pre class="lang-js prettyprint-override"><code>import { app } from &quot;../../src/application.js&quot;;
  
  import { describe, expect, test } from &quot;@jest/globals&quot;;
  import request from &quot;supertest&quot;;
  
  describe(&quot;Customer Handler&quot;, () =&gt; {
    test(&quot;should retrieve all existing customer(s)&quot;, async () =&gt; {
      const expected = [
         // ...real values here; omitted for brevity
      ];
      const response = await request(app).get(`/api/customers`);
      expect(response.statusCode).toStrictEqual(200);
      expect(response.headers[&quot;content-type&quot;]).toContain(&quot;application/json&quot;);
      expect(response.body).toStrictEqual(expected);
    });
  });
  </code></pre>
  <p>The <code>application.js</code> file looks very much like a usual <code>Express</code> setup/configuration file:</p>
  <pre class="lang-js prettyprint-override"><code>import { CustomerHandler } from &quot;./handler/customer.handler.js&quot;;
  import connection from &quot;../knexfile.js&quot;;
  
  import express from &quot;express&quot;;
  import Knex from &quot;knex&quot;;
  import { Model } from &quot;objection&quot;;
  
  const app = express();
  
  Model.knex(Knex(connection[process.env.NODE_ENV]));
  
  // ...removed helmet and some other config for brevity
  app.use(express.json());
  app.use(express.urlencoded({ extended: true }));
  app.use(&quot;/api/customers&quot;, CustomerHandler);
  app.use((err, req, res, next) =&gt; {
    res.status(err.status || 500);
    res.json({
      errors: {
        error: err,
        message: err.message,
      },
    });
    next();
  });
  
  export { app };
  </code></pre>
  <p>I've tried <code>--detectOpenHandles</code>, but nothing else is printed in the console, so I can't see any hints about what the issue might be — which I suspect it's <code>Knex</code> / <code>Objection</code> because I'm using <code>SQLite</code>, so probably the connection to the database is not getting closed.</p>
  <blockquote>
  <p>In the meantime I'm using <code>--forceExit</code>, but I would like to figure it out why is <code>Jest</code> not able to exit.</p>
  </blockquote>