sequelize: Sequelize with postgres leaves jest hanging after all tests executed
Hello, I have made a sscee for this issue:
- clone the following repo: https://github.com/LaurentVB/sequelize-hanging-test
npm install
- setup an empty postgresql DB named ‘test’, accessible by ‘root’ without password
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)
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
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
andexpress: 4.16
, I fixed it:@janmeier I’m getting
(node:50842) MaxListenersExceededWarning: Possible EventEmitter memory leak detected. 11 end listeners added. Use emitter.setMaxListeners() to increase limit
error callingsequelize.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.