3

I'm using supertest with jest to test a small KOA API that uses mongoose. Here is the test code:

const app = require('./app')
const request = require('supertest')

test('respond with json', async () => {
  return request(app.callback())
    .get('myurl')
    .set('Accept', 'application/json')
    .expect('Content-Type', /json/)
    .expect(200)
})

If I run jest with npm test I got this error (tests passes but Jest does not exit successfuly):

Jest did not exit one second after the test run has completed.

This usually means that there are asynchronous operations that weren't stopped in your tests. Consider running Jest with --detectOpenHandles to troubleshoot this issue.

According to this answer, I need to close the mongoose connection.

So I tried this:

const app = require('./app')
const request = require('supertest')
const mongoose = require('mongoose')

test('respond with json', async () => {
  return request(app.callback())
    .get('myurl')
    .set('Accept', 'application/json')
    .expect('Content-Type', /json/)
    .expect(200)
})

afterAll(() => mongoose.disconnect())

... but it does not seems to work, because I have a new error:

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)
...

And I still have this message:

Jest did not exit one second after the test run has completed.

This usually means that there are asynchronous operations that weren't stopped in your tests. Consider running Jest with --detectOpenHandles to troubleshoot this issue.

I don't understand why I have these errors and how I can avoid it.

I can edit my answer if required!

rap-2-h
  • 30,204
  • 37
  • 167
  • 263
  • `MongoError: topology was destroyed` likely means that the connection was destroyed too early. `request` is async but you don't wait for it to end. It's a promise. It likely should be `return request(app.callback())`. – Estus Flask Jul 02 '18 at 09:58
  • Thank you. I still have the `MongoError: topology was destroyed` with a `return request` (I tried with and without the `afterAll`. I edited the question to add the `return` – rap-2-h Jul 02 '18 at 10:03
  • Then it likely depends on what's going on in your app. Probably the problem is there. See for example, https://stackoverflow.com/questions/36029595/mongodb-throwing-error-while-saving-data-topology-destroyed . I would expect the code you've posted to work if everything is done properly. You can also try --detectOpenHandles like Jest suggests to check what are these handles. – Estus Flask Jul 02 '18 at 10:07
  • 5
    I'm seeing the exact same behavior. I await the asynchronous opening of a mongoose connection. After that I call disconnect on the connection. I don't do anything with the DB other than open it. I see the message with --detectOpenHandles too. And when I try to run it with this flag, the test runs, but there is no real output. I just says it ran the test. – lostdorje Jul 08 '18 at 10:56
  • 1
    I'm using mysql2 and facing the exact same error messages. Currently, using `--forceExit` flag. Not really happy with the workaround. – JeffMinsungKim Jul 11 '18 at 06:46
  • 1
    You might try wrapping your async/await function in a try/catch block. – Isaac Pak Oct 27 '18 at 01:42
  • Did you find a way to solve this? – elMeroMero Apr 28 '21 at 09:24
  • Same issue, using express.js with mongoose and jest throws `TLSWRAP` caught by `--detectOpenHandles`. Would love to know if there's a workaround for this. – unspeakable29 Oct 17 '21 at 04:16

0 Answers0