我使用 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()
会话中间件中的 -function 引起的:
// ...
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 钩子关闭了。