我的 NestJs 应用程序中有此服务:
async testTEst(id: string) {
const user = await this.prismaService.user.findUnique({
where: {
id,
},
});
// create 5sec timer
let seconds = 5;
const timer = setInterval(() => {
console.log(`Hello ${user.email}, ${seconds} seconds left`);
seconds -= 1;
if (seconds < 0) {
clearInterval(timer);
}
}, 1000);
return {
message: 'User will be deleted in 15 seconds',
};
}
我为此创建了一个集成测试,测试服务如何与所有依赖项和数据库交互。
it('should notify about user deletion countdown', async () => {
jest.useFakeTimers();
const u = await prismaService.user.create({
data: { email: '[email protected]', password: '234' },
});
const response = userService.testTEst(u.id);
jest.advanceTimersByTime(5000);
const result = await response; // Ensure the async operation is completed
expect(result).toEqual({
message: 'User will be deleted in 15 seconds',
});
});
运行测试我得到:
thrown: "Exceeded timeout of 5000 ms for a test.
。 我尝试过这个:
testTimeout: 20000
但它不起作用。 你确定第一个承诺得到解决了吗?创建用户的那个。