【发布时间】: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