I am going to test backend server running with Jest.
It success sometimes, but other times it shows error like this.

So if I use --detectOpenHandles flag as suggested, It always success without showing eny error. This is test code.
it("should be able to initialize a server (development)",async (done) => {
// Before main() is called there is no active connection:
expect(connection.readyState).toBe(0);
return main({
env: "dev",
port: PORT,
})
.then(async (server: ApolloServer) => {
// After main() got called, there is an active connection:
expect(connection.readyState).toBe(1);
await server.stop();
done();
})
});
afterAll(async () => {
await connection.close(); //connection is mongoose.connection
});
I am not sure why it fails when flag. And it weird it sometimes success, and other times fails.
Thanks