When I run the following test:
afterAll(async () => {
await runDbBuild();
await pool.end();
});
describe("queries.newteetypes.select.all():", () => {
test("Test 1: object keys ", async () => {
const awaitedResponse = await queries.newteetypes.select.all();
expect(awaitedResponse).toStrictEqual(anticipatedResponse);
});
});
I get this error: Jest has detected the following 1 open handle potentially keeping Jest from exiting:
The above code that is shown works absolutely fine in production. The issue is only with jest
giving me a potential open handles warning.
I can fix it by introducing a delay with a setTimeout()
and Promise
, but the placement baffles me. Here's the solution (same code but with one line added above the query call):
afterAll(async () => {
await runDbBuild();
await pool.end();
});
describe("queries.newteetypes.select.all():", () => {
test("Test 1: object keys ", async () => {
await new Promise((resolve) => setTimeout(() => resolve(), 2000)); // Putting a delay before the query solves the problem, but putting the delay after the query doesn't resolve it.
const awaitedResponse = await queries.newteetypes.select.all();
expect(awaitedResponse).toStrictEqual(anticipatedResponse);
});
});
This seems very counter-intuitive to me. Initially I tried putting the timeout in the afterAll()
function, but it didn't make any difference no matter where I included it. So then I experimented by putting it after the query call, within the test()
function, but that didn't work. When I put it before the query call, I stop receiving the error. But why would that make a difference?