title: Testing with Jest using mongoose: "there are asynchronous operations that weren't stopped in your tests"
body: <p>I'm using <code>supertest</code> with <code>jest</code> to test a small KOA API that uses <code>mongoose</code>. Here is the test code:</p>
  
  <pre><code>const app = require('./app')
  const request = require('supertest')
  
  test('respond with json', async () =&gt; {
    return request(app.callback())
      .get('myurl')
      .set('Accept', 'application/json')
      .expect('Content-Type', /json/)
      .expect(200)
  })
  </code></pre>
  
  <p>If I run <code>jest</code> with <code>npm test</code> I got this error (tests passes but Jest does not exit successfuly):</p>
  
  <blockquote>
    <p>Jest did not exit one second after the test run has completed.<br><br>
    This usually means that there are asynchronous operations that weren't stopped in your tests. Consider running Jest with <code>--detectOpenHandles</code> to troubleshoot this issue.</p>
  </blockquote>
  
  <p>According to <a href="https://stackoverflow.com/a/51104490/978690">this answer</a>, I need to close the <code>mongoose</code> connection.</p>
  
  <p>So I tried this:</p>
  
  <pre><code>const app = require('./app')
  const request = require('supertest')
  const mongoose = require('mongoose')
  
  test('respond with json', async () =&gt; {
    return request(app.callback())
      .get('myurl')
      .set('Accept', 'application/json')
      .expect('Content-Type', /json/)
      .expect(200)
  })
  
  afterAll(() =&gt; mongoose.disconnect())
  </code></pre>
  
  <p>... but it does not seems to work, because I have a new error:</p>
  
  <pre><code>console.error node_modules/jest-jasmine2/build/jasmine/Env.js:157
      Unhandled error
  console.error node_modules/jest-jasmine2/build/jasmine/Env.js:158
      Error [ERR_UNHANDLED_ERROR]: Unhandled error. (MongoError: topology was destroyed)
      at Function.emit (events.js:171:17)
      at done (/x/node_modules/mongoose/lib/model.js:1076:13)
      at /x/node_modules/mongoose/lib/model.js:1118:16
      at /x/node_modules/mongoose/lib/utils.js:447:16
      at err (/x/node_modules/mongodb/lib/utils.js:415:14)
      at session.endSession (/x/node_modules/mongodb/lib/utils.js:399:27)
      at ClientSession.endSession (/x/node_modules/mongodb-core/lib/sessions.js:72:41)
      at executeCallback (/x/node_modules/mongodb/lib/utils.js:397:17)
      at err (/x/node_modules/mongodb/lib/utils.js:415:14)
      at executeCallback (/x/node_modules/mongodb/lib/utils.js:404:25)
  ...
  </code></pre>
  
  <p>And I still have this message:</p>
  
  <blockquote>
    <p>Jest did not exit one second after the test run has completed.<br><br>
    This usually means that there are asynchronous operations that weren't stopped in your tests. Consider running Jest with <code>--detectOpenHandles</code> to troubleshoot this issue.</p>
  </blockquote>
  
  <p>I don't understand why I have these errors and how I can avoid it.</p>
  
  <p><em>I can edit my answer if required!</em></p>