![](/img/trans.png)
[英]I am getting a timeOut error on a jest test: thrown: Exceeded timeout of 5000 ms for a test
[英]jest timeout error: Exceeded timeout of 5000 ms for a test
我為 controller 和 mocking 創建了一個單元測試用例,請求、響應和下一個在運行測試用例時拋出了開玩笑超時錯誤。
任何人都可以幫我解決這個問題。
員工.controller.ts
import { Request, Response, NextFunction } from 'express';
import { employeeService } from './employee._service';
const empServices = new employeeService();
module employeeController {
export async function getEmployee(req: Request, res: Response, next: NextFunction) {
try {
const result = await empServices.getEmployee();
res.send(result);
return result;
}
catch (err) {
console.log(err);
res.statusCode = 200;
res.send("error in getEmployee: " + err);
};
};
}
export { employeeController }
員工.controller.spec.ts
import { Request, Response, NextFunction } from 'express';
import { employeeController } from '../employee.controller';
import { employeeService } from '../employee._service';
describe("should return pong message", () => {
const service = new employeeService();
it("should return pong message", async () => {
const mockRequest: any = {
body: jest.fn(),
params: jest.fn()
};
const mockResponse: any = {
json: jest.fn(),
status: jest.fn(),
};
const mockNext: NextFunction = jest.fn();
const spy = jest.spyOn(service, 'getEmployee').mockResolvedValueOnce([]);
const comments = await employeeController.getEmployee(mockRequest, mockResponse, mockNext);
expect(comments).toEqual([]);
expect(spy).toHaveBeenCalledWith()
expect(spy).toHaveBeenCalledTimes(1)
});
});
謝謝
一種對我有用的方法是將超時值增加到適合您需要的數字; 在本例中為 60 秒:
beforeEach((): void => {
jest.setTimeout(60000);
...
});
對於 CRA 用戶,我們可以將jest.setTimeout(ms)
放在setupTests
文件中,以在其他測試因相同原因失敗的情況下全局增加超時。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.