-
-
Notifications
You must be signed in to change notification settings - Fork 6.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
afterAll fails to close Express server and leaves open handles #9227
Comments
After following @SimenB's advice here, I converted my beforeAll and afterAll hooks to use const { createServer } = require('http')
let axios = require('axios')
const getPort = require('get-port')
const { app } = require('../../index')
const { Todo } = require('../../models')
let server
beforeAll((done) => {
getPort()
.then(port => {
axios = axios.create({ baseURL: `http://localhost:${port}` })
server = createServer(app)
server.listen(port, done)
})
})
afterAll((done) => {
server.close(done)
})
describe('GET /todos', () => {
it('returns a list of todos', async () => {
const { data: todos } = await axios.get('/todos')
todos.forEach(todo => {
expect(() => Todo.fromJson(todo)).not.toThrow()
})
})
}) |
This is because diff --git i/tests/integration/todos.test.js w/tests/integration/todos.test.js
index d5e79d5..a37475e 100644
--- i/tests/integration/todos.test.js
+++ w/tests/integration/todos.test.js
@@ -2,6 +2,7 @@ let axios = require('axios')
const getPort = require('get-port')
const { app } = require('../../index')
const { Todo } = require('../../models')
+const knex = require('../../db')
let server
@@ -13,6 +14,7 @@ beforeAll(async () => {
afterAll(() => {
server.close()
+ knex.destroy()
})
describe('GET /todos', () => { Note that you should probably wait for both We have a guide for mongodb - I'm sure knex is similar enough that it can be applied https://jestjs.io/docs/en/mongodb EDIT: That guide is just a preset, not as general as I thought it was. There is an example in this repo though, which has all the code for it: https://github.com/facebook/jest/tree/77c3ceedfd61ddc841e11fec7b76e540924d3e60/examples/mongodb. Maybe use that as basis to create a similar preset for knex? Dunno 🙂 |
@SimenB thank you for making me aware of the Update: |
Yeah, sounds good! Happy to take any other doc updates to make it clearer as well |
On a sidenote, jest-circus looks absolutely fascinating. The ability to act on any one of these Jest events looks very powerful! |
This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs. |
🐛 Bug Report
Jest fails to close the Express server in the afterAll hook and leaves an open handle. After running into this issue with both supertest and axiosist, and then trying to roll my own solution, I'm starting to think this is actually an issue with Jest.
To Reproduce
jest-github-issue
branchnpm install
.env
file containingNODE_ENV=test
npm test
Expected behavior
Jest should close the Express server in the afterAll hook and not leave any open handles
Link to repl or repo (highly encouraged)
https://github.com/johncmunson/todo-api
Be sure to checkout the
jest-github-issue
branchenvinfo
The text was updated successfully, but these errors were encountered: