jest and mongoose - jest has detected opened handles

前端 未结 2 2138
面向向阳花
面向向阳花 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:43

    It seems your mongoose connection remains open after your test, try one of the following:

    1. close server instance after test.

      const server = require('./app'); //server instance    
      server.close(); //put in afterAll or afterEach depending on your test
      
    2. close your database connection after all your test.

      afterAll(()=>{ mongoose.connection.close();});
      
    3. wrap your mongoose connection with async/await.

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

    try one or a combination. These are my solutions since I can't really see your code.

提交回复
热议问题