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

时间:2018-06-04 19:33:03

标签: mongoose jestjs

所以我使用public class CustomSignInManager : SignInManager<CustomUser, String> { private readonly ICustomUserStore _userStore; public CustomSignInManager( CustomUserManager userManager, IAuthenticationManager authenticationManager ICustomUserStore userStore ) : base(userManager, authenticationManager) { this._userStore = userStore; } /// Provided by the ASP.NET MVC template. public override Task<ClaimsIdentity> CreateUserIdentityAsync(CustomUser user) { return user.GenerateUserIdentityAsync(this.UserManager); } public override Task<SignInStatus> PasswordSignInAsync(String userName, String password, Boolean isPersistent, Boolean shouldLockout) { UserInfo userInfo = // Call method the retrieve user info from eg. the database. if (null == userInfo) { return Task.FromResult(SignInStatus.Failure); } // Do password check; if not OK: // return Task.FromResult(SignInStatus.Failure); // Password is OK; set data to the store. this._userStore.CreateOrUpdate(userInfo); // Execute the default flow, which will now use the IUserStore with the user present. return base.PasswordSignInAsync(userName, password, isPersistent, shouldLockout); } } 来测试我的node.js应用程序并且测试结果很好但是我从jest获取有关打开句柄的消息。任何见解?

  
    

jest --detectOpenHandles

  
     

PASS src / libs / user / 测试 /user_model_test.js PASS   src / 测试 /app_test.js通过   SRC /库/用户/的测试 /user_service_test.js

     

测试套房:3次通过,共3次测试:14次通过,共14次   快照:0总时间:7.209s跑完所有测试套件。

     

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

     

●承诺

jest
     

●承诺

  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)
     

●承诺

   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)
     

●承诺

   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)

2 个答案:

答案 0 :(得分:2)

它与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)

答案 1 :(得分:0)

您的猫鼬连接似乎在测试后仍然保持打开状态,请尝试以下操作之一:

  1. 测试后关闭服务器实例。

    const server = require('./app'); //server instance    
    server.close(); //put in afterAll or afterEach depending on your test
    
  2. 在完成所有测试后关闭数据库连接。

    afterAll(()=>{ mongoose.connection.close();});
    
  3. 用异步/等待包装猫鼬连接。

    async function(){
       await mongoose.connect(mongoDB);
    };
    

尝试一种或多种组合。 这些是我的解决方案,因为我看不到您的代码。