Connect-mongo 包与 Jest 导致错误“测试运行完成后,Jest 没有退出一秒” ...我收到此错误:

问题描述

我使用 express-session 包构建了一个用于基于会话的身份验证的中间件,作为会话存储,我使用了 connect-mongo 包。

当我运行这个 Jest 测试时:

//..
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
})

...我收到此错误

 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.

..并且该错误是由会话中间件中的 MongoStore.create() 函数引起的:

// ...
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: {
        // ...
})

会不会是 MongoStore 正在打开一个连接,需要在 afterEach 钩子中主动关闭?如果是,我怎么能关闭由节点模块而不是我的自定义代码打开的连接?

更新:

问题确实是 Mongo 连接保持打开状态。 我通过更改 MongoStore 以使用现有的 Mongoose 连接而不是创建自己的连接来解决该问题。这样会话存储的连接就用现有的 afterEach 钩子关闭了。

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)