-
-
Notifications
You must be signed in to change notification settings - Fork 6.5k
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
Open handle when using http.Server.listen #8554
Comments
Close the listening instance can solve the problem, hope this help. server.js const server = app.listen(port, function (err) {
if (err) {
console.log(err);
} else {
console.log('Listening on port ' + port);
}
});
module.exports = {
server
}; test.js const { server } = require('../index');
afterAll(async () => {
server.close();
}); |
@EasonWang01 I am doing this but still got the same error My only way of solving that was to do this: afterAll(done => {
httpServer.close(() => {
setTimeout(done, 100)
})
}) |
I have tried all the solutions above, I still get the error:
anyone with good luck? |
I switched from supertest to axios-test-instance (written by me). I haven’t ran into this issue since then. |
Somehow I run into the problem that jest "detects" or "has" open handles when using mongo-memory-server and supertest. With my supertest problem, e.g. I tried calling "end" but then supertest says that it's already closed - but jest still thinks there is something open - I just want to understand at which point in time or which part is having the problem. So my "wish" would be to add some support in jest to be able dive deeper into the details. |
Ran into this today and spent some time diving in — it looks like there are 3 intertwined issues here, some in Jest and some in Supertest:
(2) seems like the most immediately applicable thing here (wait a tick for any handles that are scheduled for destruction to actually be destroyed). (3) seems like it should be fixed, too, although it doesn’t really address the immediate problem here (instead, it would just make this warning show up more). |
I stumbled upon this today. Oddly enough, in my case, it doesn't always happen. If using |
FWIW, the |
@Mr0grog You might be right here. Take a look at my example on the quoted comment above. There are no other resources that may introduce unpredictable behaviour - however, if you uncomment the 'use strict'
const request = require('supertest')
const express = require('express')
test('should not leak memory', async () => {
const app = express()
await request(app).get('/')
})
// afterAll(done => setTimeout(done, 0)) That being said, |
Some types of async resources in Node.js are not destroyed until *after* their `close` or `end` or similar callbacks and events run, leading to a situation where the `--detectOpenHandles` option can falsely flag resources that have been cleaned up in user code and are already scheduled for destruction. For example, if a test ends from the callback to `TCPServer.close()` and no other tests or lifecycle functions impose additional delays, `--detectOpenHandles` will flag the server even though it has been closed. This is the main cause of issues people encounter with Supertest (see jestjs#8554). This addresses the issue by adding a short delay before collecting open handles. Depends on jestjs#11382.
Some types of async resources in Node.js are not destroyed until *after* their `close` or `end` or similar callbacks and events run, leading to a situation where the `--detectOpenHandles` option can falsely flag resources that have been cleaned up in user code and are already scheduled for destruction. For example, if a test ends from the callback to `TCPServer.close()` and no other tests or lifecycle functions impose additional delays, `--detectOpenHandles` will flag the server even though it has been closed. This is the main cause of issues people encounter with Supertest (see jestjs#8554). This addresses the issue by adding a short delay before collecting open handles. Depends on jestjs#11382.
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
I think I ran into ladjs/supertest#520. Trying to work around this, I got to a minimal example, which I think is actually a bug in Jest.
Jest detects open handles when an http server is started, even when it is closed afterwards.
To Reproduce
Create the following zero-configuration test case.
When jest is run using
--detectOpenHandles
, this will output the following:However, when the “0 ms sleep” line at the end is uncommented, no open handles are detected.
Expected behavior
Jest should not detect any open handles.
Run
npx envinfo --preset jest
The text was updated successfully, but these errors were encountered: