Jest has detected the following 1 open handle potentially keeping Jest from exiting Jest has detected the following 1 open handle potentially keeping Jest from exiting express express

Jest has detected the following 1 open handle potentially keeping Jest from exiting


you need to call done() in the end() method

const request = require("supertest");const app = require("../app");let server = request(app);it("should return 404", done =>    server        .get("/")        .expect(404)        .end(done);});


This trick worked;

afterAll(async () => {    await new Promise(resolve => setTimeout(() => resolve(), 500)); // avoid jest open handle error});

As described in this github issue.


As a general tip to debug this error, add --detectOpenHandles to your npm script that runs Jest e.g.

   "scripts": {    ...    "test": "jest --detectOpenHandles"    }

This should tell you exactly which part of the code is causing the issue (probably some type of server connection, particularly if its async).

In general, if you can move the connection code to a separate function in a file outside of your tests, then import and call it in your tests, this will also fix the issue.