【问题标题】:Jest has detected the following 1 open handle potentially keeping Jest from exiting: TCPSERVERWRAPJest 检测到以下 1 个可能阻止 Jest 退出的打开句柄:TCPSERVERWRAP
【发布时间】:2021-09-26 22:54:11
【问题描述】:

我在这里做一个基本的端到端测试,目前它失败了,但首先我无法摆脱打开的句柄。

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',
        })
      })
  })
})

知道是什么阻碍了测试运行器吗?

请注意,由于我使用的是 NestJs,因此我不需要在测试结束时使用 .end(done) 方法。

PS:显然我在这个问题上有很多代码,我需要添加更多细节,但不知道我还能说什么。

【问题讨论】:

    标签: typescript jestjs graphql nestjs supertest


    【解决方案1】:

    //在每个测试中

    it('the description', (done) => {
            request(app)
              .get('/some-path')
              .end(done);
      });
    
    

    【讨论】:

    • 正如目前所写,您的答案尚不清楚。请edit 添加其他详细信息,以帮助其他人了解这如何解决所提出的问题。你可以找到更多关于如何写好答案的信息in the help center
    【解决方案2】:

    尝试使用test 而不是it 并将done 作为参数传递并调用它。这对我有用。

    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()
      })
    

    【讨论】:

    • 这里没有乐趣。到目前为止,只有 --forceExit 选项对我有用。
    【解决方案3】:

    我仍然没有找到完美的解决方案,但目前我采用了这种解决方法:

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

    forceExit 选项以某种方式杀死 openHandles 并解锁所有内容。 然而,我仍在寻找处理该问题的“正确方法”。

    【讨论】:

    • 非常感谢,我花了几个小时试图模拟导致问题的 setInterval() 函数,但它没有工作。这是一个简单的解决方法。
    【解决方案4】:

    您正在重新创建整个应用程序 beforeEach,但仅在 afterAll 中将其拆除,这意味着您可能在此过程中泄漏了一些内存。您正在为 app 变量分配一个新实例,但很可能存在阻止垃圾收集器清除前一个实例的隐藏引用 - 例如 request 函数获得的引用。

    beforeEach 更改为beforeAll,您应该一切顺利。

    【讨论】:

    • 感谢您的回答。但不幸的是它没有效果。
    • @AMehmeto 嗯,奇怪,你是并行运行多个测试还是只运行一个文件?
    • 只有一个文件。
    • @AMehmeto 我的第二个猜测是您的应用程序中发生了一些事情,导致它无法退出。你有read through this thread,特别是链接的评论吗?
    猜你喜欢
    • 2019-03-13
    • 2020-09-20
    • 2020-08-24
    • 1970-01-01
    • 1970-01-01
    • 2019-06-30
    • 2021-06-13
    • 2020-11-13
    • 2018-11-14
    相关资源
    最近更新 更多