Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

84
Visualizações
NodeJs using pg client - Jest has detected the following open handle potentially keeping Jest from exiting - TCPWRAP

In some integration tests in my Node.js, I use pg to perform some cleanup of the Postgres tests database after the test have run. I call this in the afterAll():

afterAll(() => {

  const { Pool } = require('pg')
  const connectionString = 'postgresql://' + PG_USER + ':' + PASSWORD + '@' + HOST + ':' + PG_PORT + '/' + DATABASE_TEST;
  const pool = new Pool({
   connectionString,
   })

   pool.query('TRUNCATE someTable RESTART IDENTITY CASCADE;', (err, res) => {
       pool.end();
   })

When I run Jest tests in my Node.js app, I'm getting the error:

Jest did not exit one second after the test run has completed.

This usually means that there are asynchronous operations that weren't stopped in your tests. Consider running Jest with `--detectOpenHandles` to troubleshoot this issue.

When I add --detectOpenHandles to my npm test script in package.json, I then get the following:

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

  ●  TCPWRAP

> 259 |   pool.query('TRUNCATE someTable RESTART IDENTITY CASCADE;', (err, res) => {
      |        ^
  260 |       
  261 |       pool.end();
  262 |   })

If I move the connection to the database into a separate file (outside of the __tests__ folder), the Jest tests still don't exit (with the same error).

If I use a pg Client and do this, I get a similar issue with the client.connect()

  client.connect()
  client
    .query('TRUNCATE someTable RESTART IDENTITY CASCADE;')
    .then(() => client.end())

Can anyone explain what's actually going on here? Does Jest try to close after the final test, but the operation in afterAll() is preventing this? What do I need to do to allow the tests to exit? None of the solutions in other similar SO questions apply to my case.

Update

I tried the suggestion by @Estus Flask, but it still doesn't fix the issue. Using the following (which passes done as the callback function, so that the afterAll won't complete until done() is called (which I call when the pool.end() promise resolves):

afterAll( done => {

  const { Pool } = require('pg')
  const connectionString = 'postgresql://' + PG_USER + ':' + PASSWORD + '@' + HOST + ':' + PG_PORT + '/' + DATABASE_TEST;
  const pool = new Pool({
   connectionString,
   })

   pool.query('TRUNCATE someTable RESTART IDENTITY CASCADE;', (err, res) => {
     pool.end().then(done());
   })
  })
});
over 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

I'm not sure if I'm correct but it doesn't seem like your pool.end awaited nor via await or via the old-fasion promise. Try this-

afterAll(async () => {

    const { Pool } = require('pg')
    const connectionString = 'postgresql://' + PG_USER + ':' + PASSWORD + '@' + HOST + ':' + PG_PORT + '/' + DATABASE_TEST;
    const pool = new Pool({
    connectionString,
    })
    
    await pool.query('TRUNCATE someTable RESTART IDENTITY CASCADE;')
    await pool.end()
}

Furthermore, as stated below postgres has a limited amount of connections, so, it's always a good practice to close connections, especially on tests.

By default, all PostgreSQL deployments on Compose start with a connection limit that sets the maximum number of connections allowed to 100.

over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda