gpt4 book ai didi

jestjs - 嵌套 e2e 测试 : Jest did not exit one second after the test run has completed

转载 作者:行者123 更新时间:2023-12-05 01:09:16 40 4
gpt4 key购买 nike

我在 nestJs 微服务中进行了一些 e2E 测试(使用 Redis 作为传输器)。一切都很顺利,只是这个过程永远不会结束。

这是控制台上显示的消息

Ran all test suites.
Jest did not exit one second after the test run has completed.

This usually means that there are asynchronous operations that weren't stopped in your tests. Consider running Jest with `--detectOpenHandles` to troubleshoot this issue.

这是我的 Controller 的代码

import {Body, Controller, HttpCode, Post, Query} from '@nestjs/common';
import { AppService } from './app.service';
import {Client, ClientProxy, MessagePattern, Transport} from '@nestjs/microservices';
import {AdminDto} from "./admin.dto";
import {Admin} from "./admin.interface";
import {Observable} from "rxjs";

@Controller()
export class AppController {
constructor(private readonly appService: AppService) {}

/** Useful for test only **/
@Client({ transport: Transport.REDIS })
client: ClientProxy;

@Post()
@HttpCode(200)
call(@Query('command') cmd, @Body() data: any): Observable<any> {
return this.client.send<number>({ cmd }, data);
}
/** End of test **/

@MessagePattern({cmd: 'createAdmin'})
async createClient(adminDto: AdminDto): Promise<Admin> {
return await this.appService.create(adminDto);
}
}

这是我的 app.e2e-soec.ts 文件,如您所见,我关闭了 afterEach 功能上的所有连接。

import { Test } from '@nestjs/testing';
import { INestApplication } from '@nestjs/common';
import * as request from 'supertest';
import {Transport} from "@nestjs/microservices";
import {AppModule} from "../src/app.module";
import * as mongoose from "mongoose";
import {connection} from "mongoose";

describe('AppController (e2e)', () => {
let server;
let app: INestApplication;

beforeEach(async () => {
const module = await Test.createTestingModule({
imports: [AppModule],
}).compile();

app = module.createNestApplication();
server = app.getHttpAdapter().getInstance();

app.connectMicroservice({
transport: Transport.REDIS,
options: {
url: 'redis://0.0.0.0:6379',
},
});
await app.startAllMicroservicesAsync();
await app.init();
});

it(`/POST (create admin)`, done => {
const adminDto = {
firstName : 'test',
lastName: 'toto',
password: '1234',
email: 'test@toto.fr',
roles: ['ROLE_ADMIN']
};

return request(server)
.post('/?command=createAdmin')
.send(adminDto)
.end((error, response) => {
expect(response.status).toEqual(200);
expect(response.body).toMatchObject({
_id: expect.any(String),
firstName: "test",
lastName: "toto"
});
done();
});
});

afterEach(async done => {
await mongoose.connection.close();
await connection.close();
await app.close();
done();
});
});

编辑

按照 Jesse Carter 的建议,我添加了 leaked-handles 以获得更多线索

好像是因为redis

tcp handle leaked at one of: 
at RedisClient.Object.<anonymous>.RedisClient.create_stream (/Users/myUser/project/admin-service/node_modules/redis/index.js:195:31)
tcp stream {
fd: 38,
readable: true,
writable: true,
address: { address: '127.0.0.1', family: 'IPv4', port: 54468 },
serverAddr: null
}

最佳答案

可能晚了,但你必须从模块变量中调用 close 函数,如下所示:

afterEach(async done => {
for (const connection of connections) {
await connection.close();
}
await app.close();
await module.close(); // <-- this line
done();
});

这对我有用。

关于jestjs - 嵌套 e2e 测试 : Jest did not exit one second after the test run has completed,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65352747/

40 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com