sequelize: Sequelize with postgres leaves jest hanging after all tests executed

Hello, I have made a sscee for this issue:

  1. clone the following repo: https://github.com/LaurentVB/sequelize-hanging-test
  2. npm install
  3. setup an empty postgresql DB named ‘test’, accessible by ‘root’ without password
  4. npm test

What do you expect to happen?

The test executes and the prompt returns.

What is actually happening?

After all tests pass, the execution of jest hangs until it’s killed with CTRL+C. The same test with an embedded in-memory sqlite DB behaves as expected.

Dialect: postgres Sequelize version: 3.24.6

About this issue

  • Original URL
  • State: closed
  • Created 8 years ago
  • Reactions: 4
  • Comments: 18 (4 by maintainers)

Most upvoted comments

This is not working anymore. Using latest version of jest and sequelize, jest still hangs after calling sequelize.close()

I found a solution, which was to leverage Jest’s setupFilesAfterEnv to run afterAll(() => sequelize.close()) on every test suite.

I know this is similar to what others have suggested, but by putting it inside setupFilesAfterEnv it feels like a more appropriate solution since it hits every test without violating DRY.

Hmm yeah, I see that behaviour as well - I’ve never seen it happen in mocha tests, so it must be something jest is doing 😃

Adding

afterAll(function () {
 sequelize.close();
});

works fine though

@julienvincent I figured out my problem. For me it is the models sync that does not exit. In my test setup I do:

const models = require(‘…/src/db/models’); beforeAll(() => { models.sequelize.sync().then(()=>{ models.sequelize.close(); }) });

I just used the --forceExit option and it fixed the freezing issue.

Details: Doing a sequelize.close() was causing an issue as it needed to be in the afterAll method. But this method had to run after all tests were done. Getting this configured was getting too difficult so I just used the --forceExit flag in Jest and it seems to work.

Using jest: 22.4, sequelize: 4.37 and express: 4.16, I fixed it:

// on the tests
afterAll(() => app.close())  // NOTE: there is NO "end" callback passed

// on app.js
app.on('close', () => sequelize.close())

@janmeier I’m getting (node:50842) MaxListenersExceededWarning: Possible EventEmitter memory leak detected. 11 end listeners added. Use emitter.setMaxListeners() to increase limit error calling sequelize.close() and hanged.

This happens with tape too btw. I’ve tried calling sequelize.close() at then end of my tests as well but it does not end the test.