supertest icon indicating copy to clipboard operation
supertest copied to clipboard

open handles with body-parser#1.20.0

Open lucamartini opened this issue 2 years ago • 16 comments

Hallo,

jest test run does not exit and instead prints this error

Jest has detected the following 64 open handles potentially keeping Jest from exiting:
  ●  bound-anonymous-fn
      23 |
      24 |   const response = await request
    > 25 |     .post('/gql')
         |      ^
      26 |     .set('origin', config.devHosts[0])
      27 |     .send(loginQuery)
      28 |     .expect(200);
      at Test.serverAddress (node_modules/supertest/lib/test.js:48:35)
      at new Test (node_modules/supertest/lib/test.js:34:14)
      at Object.post (node_modules/supertest/index.js:28:14)
      at login (test/integration/helpers/login.js:25:6)
      at Object.<anonymous> (test/integration/users/usersMutations.itest.js:45:25)
          at runMicrotasks (<anonymous>)
...

We have found this happens only for the routes where we use the express body-parser middleware. express version = 4.17.3 body-parser version = 1.20.0

lucamartini avatar Apr 15 '22 07:04 lucamartini

I am also seeing this.

Jest has detected the following 1 open handle potentially keeping Jest from exiting:

  ●  TCPSERVERWRAP

      24 |       request(app)
    > 25 |           .get('/')
         |            ^
      26 |           .query({/*query data*/})
      27 |           .send()
      28 |           .expect('Location', '/error.html');

      at Test.serverAddress (node_modules/supertest/lib/test.js:48:35)
      at new Test (node_modules/supertest/lib/test.js:34:14)
      at Object.obj.<computed> [as get] (node_modules/supertest/index.js:28:14)
      at Object.<anonymous> (functions/src/express-apps/oauth-callback.spec.ts:25:12)

express = 4.17.3 supertest = 6.2.2

zachsitka avatar Apr 19 '22 15:04 zachsitka

I'm having the same lucamartini's problem, and I'm using express 4.17.3, supertest 6.2.2 and body-parser version = 1.20.0. It seems to started recently, as I'm upgrading my projects, because I had the tests for quite a while, and just recently this problems appeared. When I put a timeout in afterAll method, depending on how long I wait, the problem do not happen.

ricardomat avatar Apr 27 '22 12:04 ricardomat

@lucamartini and @ricardomat are you using a fake database? I had the same problem and then by doing my research, I have found a beautiful solution which is using the NPM package mongodb-memory-server This should be installed as development dependency!!!

wilhelm-lynch avatar Apr 27 '22 13:04 wilhelm-lynch

Hello! As a plus comment, this also happens when using morgan

bscantor23 avatar May 31 '22 15:05 bscantor23

Having the same problem

DanielFredriksson avatar Jul 14 '22 13:07 DanielFredriksson

It seems yarn add -D body-parser@^2.0.0-beta.1 fixes the problem ..

dominikwinter avatar Jul 16 '22 23:07 dominikwinter

I ran into this as well, don't feel great running beta in prod though. Other ideas? :D Is this a known issue on the body-parser side?

sshevlyagin avatar Jul 19 '22 04:07 sshevlyagin

Having the same problem

kdupont-asfotec avatar Jul 22 '22 05:07 kdupont-asfotec

Having the same problem. Can confirm that npm i -D body-parser@^2.0.0-beta.1 works for me.

saqib-ahmed avatar Jul 25 '22 10:07 saqib-ahmed

Have the same problem as well, but upgrading body-parser to 2.0.0-beta.1 did not work for me.

branberry avatar Jul 27 '22 00:07 branberry

@saqib-ahmed fixed the problem in my case..

omerlewitz avatar Jul 31 '22 20:07 omerlewitz

Can confirm that this also worked on my setup.

"body-parser": "^2.0.0-beta.1",
"jest": "^28.1.3",
"supertest": "^6.2.4",
"typescript": "^4.7.4"
"express": "^4.18.1"

I had to use body-parser package as follow:

app.use(bodyParser.json());

Just want to mention that using body-parser as such without the code above

app.use(bodyParser.urlencoded({ extended: true /**or false */ }));

will result to these errors.

Jest has detected the following 3 open handles potentially keeping Jest from exiting:

  ●  write

      17 |
      18 |         const { statusCode } = await supertest(server)
    > 19 |           .post('/users')
         |            ^
      20 |           .send(userInput);
      21 |
      22 |         expect(statusCode).toBe(200);

thisdotEric avatar Aug 06 '22 11:08 thisdotEric

^2.0.0-beta.1 It worked for me too.

Nicopa avatar Oct 04 '22 20:10 Nicopa

I have the same issue

Jest has detected the following 2 open handles potentially keeping Jest from exiting:

  ●  bound-anonymous-fn

      332 |
      333 |         const response = await agent
    > 334 |             .post(routePath)
          |              ^
      335 |             .set({authorization: token})
      336 |             .send(requestBody);
      337 |

      at Test.serverAddress (node_modules/supertest/lib/test.js:48:35)
      at new Test (node_modules/supertest/lib/test.js:34:14)
      at TestAgent.<computed> [as post] (node_modules/supertest/lib/agent.js:62:17)
      at Object.post (src/files/tests/files.v1.test.js:334:14)
          at runMicrotasks (<anonymous>)


  ●  bound-anonymous-fn

      386 |
      387 |             const response = await agent
    > 388 |                 .post(routePath)
          |                  ^
      389 |                 .set({authorization: token})
      390 |                 .send(requestBody);
      391 |

      at Test.serverAddress (node_modules/supertest/lib/test.js:48:35)
      at new Test (node_modules/supertest/lib/test.js:34:14)
      at TestAgent.<computed> [as post] (node_modules/supertest/lib/agent.js:62:17)
      at Object.post (src/files/tests/files.v1.test.js:388:18)
          at runMicrotasks (<anonymous>)

Done in 8.99s.

mohamedelbalshy avatar Jan 18 '23 15:01 mohamedelbalshy

upgrade to [email protected] and [email protected] resolved it for me.

huynq-2493 avatar Mar 10 '23 06:03 huynq-2493

upgrade to [email protected] and [email protected] resolved it for me.

You do not need to use ts-jest to reproduce this issue, so that's irrellevant. Upgrading to [email protected] does not fix the issue for me, which makes sense because Jest is not the cause of the issue. In fact, you do not even need to Supertest to reproduce this issue, the issue is somewhere in Express internals (perhaps body-parser as others have mentioned).

TL;DR we're all barking up the wrong tree here. This is a problem that Express' maintainers (who are not very active) need to solve.

ryami333 avatar Mar 10 '23 11:03 ryami333