Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

329
Vistas
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 la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda