Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

People also ask

How do you fix Jest did not exit one second after the test run has completed?

To fix the 'Jest did not exit one second after the test run has completed' error with TypeScript, we should make sure we clean up when our test is done. beforeAll((done) => { done(); }); //... afterAll((done) => { mongoose. connection.

How do I set Jest timeout?

Use jest. setTimeout(newTimeout) to increase the timeout value, if this is a long-running test." 1)Increased jest. setTimeout(30000) from 30000 to 60000.

What does done () do in Jest?

Instead of putting the test in a function with an empty argument, use a single argument called done . Jest will wait until the done callback is called before finishing the test.

Why are my Jest tests slow?

Jest is slow on Windows Unfortunately Jest tests run significantly slower on Windows machines than on Mac OS and Linux due to slower crawling of the Windows file system.


My problem was solved by this code:

beforeAll(done => {
  done()
})

afterAll(done => {
  // Closing the DB connection allows Jest to exit successfully.
  mongoose.connection.close()
  done()
})

I was having the same issue but in my package.json file i added "test": "jest --detectOpenHandles" and ran npm test --detectOpenHandles. I didn't get the error message this time. Maybe you can try doing that.


On my side, I just separate app.listen() from my app. So with express, your app finish with an export.

// index.js
module.exports = app;

And just create another file to listen the port.

// server.js
const app = require('./index')
app.listen(...)

And if you import just the index (app index.js) in your tests, it should work with no extra config. Of course your need to adjust the start of your express app. It should use now server.js.


For me it was a different issue I was using supertest to test routes itself so I had to close the connection to the server itself.

afterAll(done => {
    server.close();
    done();
});

If this is not the case for you this issue might have something for you