gpt4 book ai didi

mongoose - jest 和 mongoose - jest 检测到打开的句柄

转载 作者:行者123 更新时间:2023-12-04 02:25:43 28 4
gpt4 key购买 nike

所以我正在使用 jest测试我的 node.js 应用程序,测试完成得很好,但我从 jest 收到一条关于打开句柄的消息。任何见解?

jest --detectOpenHandles



通过 src/libs/user/ 测试 /user_model_test.js 通过
src/ 测试 /app_test.js 通过
源代码/库/用户/ 测试 /user_service_test.js

测试套件:3 次通过,总共 3 次测试:14 次通过,总共 14 次
快照:0 总时间:7.209 秒运行所有测试套件。

Jest 检测到以下 4 个可能保留的打开句柄
退出玩笑:

● promise
  2 | // we use a test database for testing
3 | var mongoDB = 'mongodb://localhost/my_db_conn';
> 4 | mongoose.connect(mongoDB);
| ^
5 | const User = require('../user_model');
6 |
7 | describe("User model test", () => {

at NativeConnection.Object.<anonymous>.Connection.openUri (node_modules/mongoose/lib/connection.js:424:19)
at Mongoose.Object.<anonymous>.Mongoose.connect (node_modules/mongoose/lib/index.js:208:15)
at Object.<anonymous> (src/libs/user/__tests__/user_model_test.js:4:10)

● promise
   8 | });
9 |
> 10 | module.exports = mongoose.model("User", UserSchema);
| ^

at Function.init (node_modules/mongoose/lib/model.js:962:16)
at Mongoose.Object.<anonymous>.Mongoose.model (node_modules/mongoose/lib/index.js:392:11)
at Object.<anonymous> (src/libs/user/user_model.js:10:27)
at Object.<anonymous> (src/libs/user/__tests__/user_model_test.js:5:14)

● promise
   8 | });
9 |
> 10 | module.exports = mongoose.model("User", UserSchema);
| ^

at Function.init (node_modules/mongoose/lib/model.js:962:16)
at Mongoose.Object.<anonymous>.Mongoose.model (node_modules/mongoose/lib/index.js:392:11)
at Object.<anonymous> (src/libs/user/user_model.js:10:27)
at Object.<anonymous> (src/libs/user/index.js:1:41)

● promise
  3 | var mongoose = require('mongoose');
4 | var mongoDB = 'mongodb://localhost/my_db_conn';
> 5 | mongoose.connect(mongoDB);
| ^
6 |
7 | describe('App test', () => {
8 | it('has a module', () => {

at NativeConnection.Object.<anonymous>.Connection.openUri (node_modules/mongoose/lib/connection.js:424:19)
at Mongoose.Object.<anonymous>.Mongoose.connect (node_modules/mongoose/lib/index.js:208:15)
at Object.<anonymous> (src/__tests__/app_test.js:5:10)

最佳答案

它与model.init有关返回 promise 的函数。快速修复将通过 skipInit在创建模型时标记如下:
const User = mongoose.model("users", userSchema, "users", true)skipInit是这个函数的第四个参数

但在这种情况下,它不会为您的模型初始化索引,因此最好根据 process.env.NODE_ENV 设置此标志。
const skipInit = process.env.NODE_ENV === "test"
const User = mongoose.model("users", userSchema, "users", skipInit)

关于mongoose - jest 和 mongoose - jest 检测到打开的句柄,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50687592/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com