6

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: screenshot of error

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?

1
  • 1
    Wow, I am fighting this exact issue. So far I found nothing to help, BUT pasting your 2 second timer above my pool.connect() solved it. Very, very odd. At any rate, I think that helps narrow it toward connection issue versus query issue.
    – alfreema
    Commented Nov 17, 2021 at 21:53

1 Answer 1

8

Treat your "new Pool( ...)" as a promise and await for it:

async function createPool () {
  return await new Pool({ connectionString })
}

Your findings of setTimeout, had me try this with the same successful result:

await process.nextTick(() => {})

Placing that right before my connect() worked. So from there I started backing up to the "earliest" point that adding a process.nextTick would fix the issue and it appears to be solved by just treating new Pool(..) as a promise.

Perhaps the pool is willing to start dishing out clients before it's completely ready to track them under the hood? That might be why the extra await up front solves this.

This could very much be related to why people are not able to close clients or the pool properly -- because something about the pool management is mangled. For example: https://github.com/brianc/node-postgres/issues/2341

7
  • Ah I'm so relieved that I'm not alone with this! I'll check your solution out when I'm looking at the back end next. Thanks for sharing - I'll let you know how it goes! Commented Nov 18, 2021 at 7:30
  • 1
    Yeah, would be helpful to know if it fixes your issue. I posted some commentary at the github site: github.com/brianc/node-postgres/issues/2648
    – alfreema
    Commented Nov 18, 2021 at 15:34
  • This doesn't seem to make sense - constructors in JavaScript can't be async (see stackoverflow.com/questions/36363278/…) so await new Pool() or await new Anything() shouldn't be any different from the same code without await. Commented Jul 8, 2022 at 15:56
  • @mikemaccana It depends on what node-postgres is doing underneath the hood doesn't it? For example: stackoverflow.com/a/50885340/2957186 I didn't dive into what node-postgres is doing, but that added await definitely solves the problem. If you dive in and find a better/more appropriate solution (probably easy to do), I'll edit this solution and point to yours.
    – alfreema
    Commented Jul 8, 2022 at 17:51
  • 1
    Thanks! I tried this with integration tests that use Axios and it works: await new Promise(resolve => process.nextTick(resolve))
    – Chakrit W
    Commented Mar 17, 2023 at 2:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.