Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

325
Views
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
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!