jest and mongoose - jest has detected opened handles

前端 未结 2 2148
面向向阳花
面向向阳花 2021-02-13 20:21

So I\'m using jest to test my node.js application and the tests finish fine but I\'m getting a message from jest about open handles. Any insights?

2条回答
  •  忘掉有多难
    2021-02-13 20:32

    It's related to model.init function which returns promise. Quick fix will be to pass skipInit flag while creating the model like this:

    const User = mongoose.model("users", userSchema, "users", true)

    skipInit is the fourth parameter in this function

    But in this case it will not initialize indexes for your model, so it's better to set this flag according to the process.env.NODE_ENV

    const skipInit = process.env.NODE_ENV === "test" const User = mongoose.model("users", userSchema, "users", skipInit)

提交回复
热议问题