Gábor Schön
Gábor Schön

Reputation: 21

How to enforce SLONIK to close open pools

THE PROBLEM

I have a NodeJs server where I'm using jest for testing. In case of integration tests i get a message after all tests where passing:

"Jest has detected the following X (15-20) open handles potentially keeping Jest from exiting"

I knew what this means, have already seen it when i was using Sequelize as ORM, but now I'm using Slonik.

I found this topic what was really useful:

https://github.com/gajus/slonik/issues/63

so when i set the idleTimeOut as advised it is solved.


test("foo", async () => {
  const slonik = createPool(
    `postgres://postgres:[email protected]:7002/postgres`,
    {
      maximumPoolSize: 1,
      minimumPoolSize: 1,
      idleTimeout: 1 // milliseconds!
    }
  );
  await slonik.many(
    sql`SELECT table_name FROM information_schema.tables WHERE table_schema='public';`
  );
});

I tried to solve this problem from an other perspective. My ide was to close the connection in the afterAll block of jest. The test setup is:

  const db = container.resolve(DbConnectionPool).connection;
  let appServer;
  let api;

  beforeEach(() => {
    appServer = app.listen(config.port);
    api = supertest(appServer);
  });

  afterEach(async () => {
    appServer.close();
    await db.query(sql`TRUNCATE reports`);
  });

What i have tried in the afterAll block:

afterAll(async () => {
    await db.end()
  });

It does not solves my problem as the documentation tells: 'Note: pool.end() does not terminate active connections/ transactions.'

I do not found anything about how to enforce the close of a pool until now.

So thought i can be tricky and i will close the connection using SQL:

afterAll(async () => {
    await db.query(sql`DISCONNECT ALL`);
  });

Does not work as well.

I still had an idea to play with. Slonik documentation tells, that the default idleTimeOut for a connection is 5000ms in default.

So i have tried to set a timeout in the afterAll block with 6000 ms, but i still get the warning from Jest.

So does anyone have any idea how to force-close the connection for my tests?

Upvotes: 2

Views: 680

Answers (0)

Related Questions