首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >Jest检测到以下1个可能使Jest无法退出的打开句柄: TCPSERVERWRAP

Jest检测到以下1个可能使Jest无法退出的打开句柄: TCPSERVERWRAP
EN

Stack Overflow用户
提问于 2021-07-19 16:57:39
回答 3查看 1.8K关注 0票数 1

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

代码语言:javascript
运行
复制
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)
代码语言:javascript
运行
复制
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:显然,我有很多关于这个问题的代码,我需要添加更多的细节,但没有线索,我可以说更多。

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2021-07-21 20:59:32

我还没有找到一个完美的解决方案,但就目前而言,我找到了这个变通方法:

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

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

票数 1
EN

Stack Overflow用户

发布于 2021-09-21 16:50:18

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

代码语言:javascript
运行
复制
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()
  })
票数 2
EN

Stack Overflow用户

发布于 2021-07-19 17:17:11

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

beforeEach更改为beforeAll,您就应该可以运行了。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/68437734

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档