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

11,832

Solution 1

I still haven't found a perfect solution, but for the moment I went for this workaround :

jest --config ./test/jest-e2e.json --forceExit

The forceExit option kill the openHandles somehow and unlock everything. Yet, I still looking for the "proper way" of handling that issue.

Solution 2

Instead of it try using test and pass done as a parameter and call that. This worked for me.

test('mutation', async (done) => {
    const mutation = {
      query: `mutation Create($title: String!) {
        create(title: $title) {
          id,
          title
        }
      }`,
      variables: {
        title: 'Mon programme',
      },
    }
    const response = request(app.getHttpServer())
      .post('/graphql')
      .send(mutation)
     expect(response).to.be(HttpStatus.Ok)
     done()
  })

Solution 3

You are re-creating the whole app again beforeEach, but tearing it down only in afterAll, which means you are probably leaking some memory along the way. You are assigning a new instance to the app variable, but there are most likely hidden references that prevent the garbage colletor from clearing the previous instance - like the reference that the request function got.

Change beforeEach to beforeAll and you should be good to go.

Share:
11,832
A Mehmeto
Author by

A Mehmeto

Don't worry about me

Updated on June 11, 2022

Comments

  • A Mehmeto
    A Mehmeto almost 2 years

    I am doing a basic end to end testing here, for the moment it's failing, but first I can't get rid of the open handle.

    Ran all test suites.
    
    Jest has detected the following 1 open handle potentially keeping Jest from exiting:
    
      ●  TCPSERVERWRAP
    
          40 |     }
          41 |     return request(app.getHttpServer())
        > 42 |       .post('/graphql')
             |        ^
          43 |       .send(mutation)
          44 |       .expect(HttpStatus.OK)
          45 |       .expect((response) => {
    
          at Test.Object.<anonymous>.Test.serverAddress (../node_modules/supertest/lib/test.js:61:33)
          at new Test (../node_modules/supertest/lib/test.js:38:12)
          at Object.obj.<computed> [as post] (../node_modules/supertest/index.js:27:14)
          at Object.<anonymous> (app.e2e-spec.ts:42:8)
    
    import { Test, TestingModule } from '@nestjs/testing'
    import { HttpStatus, INestApplication } from "@nestjs/common";
    import * as request from 'supertest'
    import { AppModule } from '../src/app.module'
    
    describe('AppController (e2e)', () => {
      let app: INestApplication
    
      beforeEach(async () => {
        const moduleFixture: TestingModule = await Test.createTestingModule({
          imports: [AppModule],
        }).compile()
    
        app = moduleFixture.createNestApplication()
        await app.init()
      })
    
      afterAll(async () => {
        await app.close()
      })
    
      it('/ (GET)', () => {
        return request(app.getHttpServer())
          .get('/')
          .expect(HttpStatus.OK)
          .expect('Hello World!')
      })
    
      it('mutation', async () => {
        const mutation = {
          query: `mutation Create($title: String!) {
            create(title: $title) {
              id,
              title
            }
          }`,
          variables: {
            title: 'Mon programme',
          },
        }
        return request(app.getHttpServer())
          .post('/graphql')
          .send(mutation)
          .expect(HttpStatus.OK)
          .expect( (response) => {
            expect(response.body).toBe({
              id: expect.any(String),
              title: 'Mon programme',
            })
          })
      })
    })
    

    Any idea what's blocking the test runner ?

    Note that, as I am using NestJs, I shouldn't need to use the .end(done) method at the end of the test.

    PS: apparently I have to much code on this question and I need to add some more details, but have no clue what I can say more.

  • A Mehmeto
    A Mehmeto almost 3 years
    Thank for the answer. But unfortunately it has no effect.
  • Papooch
    Papooch almost 3 years
    @AMehmeto hm, strange, are you running multiple tests in parallel or just one file?
  • A Mehmeto
    A Mehmeto almost 3 years
    Just one file only.
  • Papooch
    Papooch almost 3 years
    @AMehmeto my second guess is that something is happening within your app that keeps it from exiting. Have you read through this thread, expecially the linked comment?
  • Patrick
    Patrick over 2 years
    thank you so much I have spent hours trying to mock the setInterval() function causing problems but it didn't work. This is an easy, simple fix.
  • Admin
    Admin over 2 years
    As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center.
  • guero64
    guero64 over 2 years
    No joy here. So far only the --forceExit options works for me.
  • David
    David about 2 years
    it and test are the same
  • Florian Koch
    Florian Koch about 2 years
    Actually there are two approaches mixed here, either use async or the done callback. You will notice using both isn't possible in Typescript, where it refuses to run this code.
  • fadingbeat
    fadingbeat almost 2 years
    Nice job! Your hourly research has helped me :)
  • Akif Kara
    Akif Kara almost 2 years
    I had --forceExit and --detectOpenHandles in my script after removed --detectOpenHandles it solved "test:e2e": "jest --config ./test/jest-e2e.json --forceExit"