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

335
Visualizações
Connect-mongo package with Jest leads to error "Jest did not exit one second after the test run has completed"

I built a middleware for session-based authentication with express-session package and as a session store I use the connect-mongo package.

When I run this Jest test:

//..
const createServer = require("./server") // <-- this line triggers the error

beforeEach((done) => {
    mongoose.connect(
        `${DB_URL}_test_db`,
        { useNewUrlParser: true },
        () => done()
    )
})

afterEach((done) => {
    mongoose.connection.db.dropDatabase(() => {
        mongoose.connection.close(() => done())
    })
})

const app = createServer()

test("Try to access /app/hello w/o login", () => {
    expect(401).toBe(400 + 1) //<-- this test just as dummy
})

.. I get this error:

 PASS  ./server.test.js
  ✓ Try to access /app/hello w/o login (40 ms)

Test Suites: 1 passed, 1 total
Tests:       1 passed, 1 total
Snapshots:   0 total
Time:        1.832 s, estimated 2 s
Ran all test suites.
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.

..and the error is caused by the MongoStore.create()-function in the Session Middleware:

// ...
const session = require("express-session")
const MongoStore = require("connect-mongo")

module.exports = session({
    name: SESSION_NAME,
    secret: SESSION_SECRET,
    saveUninitialized: false,
    resave: false,
    store: MongoStore.create({ //<-- removing this removes the error (but I need it)
        mongoUrl: DB_URL,
        collection: "session",
        dbName: "somedbname",
        ttl: parseInt(SESSION_LIFETIME / 1000)
    }),
    cookie: {
        // ...
})

Could it be that the MongoStore is opening a connection and that it needs to be actively closed in the afterEach hook? If yes how could I close that connection which is opened by a node module and not by my custom code?

Update:

Problem was indeed the Mongo connection remaining open. I solved the issue by changing the MongoStore to use the existing Mongoose connection instead of creating an own one. This way the connection of the session store was closed with the existing afterEach hook.

over 4 years ago · Santiago Trujillo
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