Using knex.js to connect to the database seems to work fine with mocha, but with Jest for whatever reason the process hangs after running the tests. The tests seem to run successfully, and pull data. But then the process hangs.
Versions
Code
// package.json
{
"name": "node",
"version": "1.0.0",
"main": "index.js",
"license": "MIT",
"scripts": {
"test": "NODE_ENV=test ./node_modules/.bin/jest"
},
"dependencies": {
"knex": "^0.13.0",
"pg": "^6.2.3"
},
"devDependencies": {
"jest": "^20.0.4"
},
"jest": {
"testPathIgnorePatterns": [
"<rootDir>[/\\\\](seeds|docs|node_modules|migrations)[/\\\\]"
],
"testEnvironment": "node"
}
}
// index.js
const knex = require('knex')({
client: 'pg',
connection: {
host : '127.0.0.1',
user : 'aaronmcleod',
database : 'aaronmcleod'
}
})
module.exports = function () {
return knex('users')
}
// index.test.js
const fn = require('./index')
describe('fn', () => {
it('get users', (done) => {
fn().then((res) => {
expect(res.length).toBe(0)
done()
}).catch(done)
})
})
How about
.catch(done.fail)
@thymikee Doesn't change anything I'm afraid. When this happened in a project at work, I ended up removing everything from the test case, and it wasn't until i removed the code that initialized knex, the hang went away. For example:
Having this as my test case, the process still hangs:
const fn = require('./index')
describe('fn', () => {
it('get users', () => {
// fn().then((res) => {
// expect(res.length).toBe(0)
// done()
// }).catch(done.fail)
})
})
A node process won't exit while sockets are still connected. When you require your index.js, knex will setup and maintain connection pool to the database. While these connections are active, jest won't exit.
You either need to call knex.destroy in an afterAll() hook or use the --forceExit flag when starting jest.
More in the jest documentation : https://facebook.github.io/jest/docs/cli.html#forceexit
Cool, thanks @kouak !
A node process won't exit while sockets are still connected. When you require your
index.js, knex will setup and maintain connection pool to the database. While these connections are active, jest won't exit.You either need to call
knex.destroyin anafterAll()hook or use the--forceExitflag when starting jest.More in the jest documentation : https://facebook.github.io/jest/docs/cli.html#forceexit
--forceExit did it for me on NestJS
A node process won't exit while sockets are still connected. When you require your
index.js, knex will setup and maintain connection pool to the database. While these connections are active, jest won't exit.
You either need to callknex.destroyin anafterAll()hook or use the--forceExitflag when starting jest.
More in the jest documentation : https://facebook.github.io/jest/docs/cli.html#forceexit--forceExit did it for me on NestJS
I'm having the same issue: using knex.destroy in an afterAll() and with the --forceExit flag
Also, tried raising the default timeout limit to no awail
Most helpful comment
A node process won't exit while sockets are still connected. When you require your
index.js, knex will setup and maintain connection pool to the database. While these connections are active, jest won't exit.You either need to call
knex.destroyin anafterAll()hook or use the--forceExitflag when starting jest.More in the jest documentation : https://facebook.github.io/jest/docs/cli.html#forceexit