ฉันพยายามจะทดสอบฟังก์ชันที่มี setTimeout
ข้างในเป็นสัญญา แต่มันทำให้เวลานออกมา
นี่คือการตรวจสอบสิทธิ์
export const sleep = async (duration: number): Promise<void> => {
await new Promise<void>((resolve) => {
setTimeout(resolve, duration);
});
if (process.env.NODE_ENV === "test") {
console.log("sleep end");
}
};
และนี่คือของฉันทดสอบ:
import { sleep } from "../../helpers/utils";
console.log = jest.fn();
jest.useFakeTimers();
test("calls sleep with correct argument and calls console.log", async () => {
const NODE_ENV = "test";
const SLEEP_DURATION = "100";
process.env = { ...process.env, NODE_ENV, SLEEP_DURATION };
const timeoutSpy = jest.spyOn(global, "setTimeout");
await sleep(+SLEEP_DURATION);
jest.runAllTimers();
expect(sleep).toHaveBeenCalledWith(+SLEEP_DURATION);
expect(timeoutSpy).toHaveBeenCalledWith(+SLEEP_DURATION);
expect(console.log).toHaveBeenCalledWith("sleep end");
});
ปัญหาก็คือตอนที่ฉันพยายามจะวิ่งนี้การทดสอบความผิดพลาดและทำให้ข้อความนี้:
thrown: "Exceeded timeout of 5000 ms for a test.
Use jest.setTimeout(newTimeout) to increase the timeout value, if this is a long-running test."
ฉันพยายาม jest.setTimeout(10000)
ซึ่งก็แค่โยเกิดข้อผิดพลาดของ Exceeded timeout of 10000ms ...
รู้มั้ยว่าทำไมแม่คนนี้ถึงเกิดขึ้น? หรือว่าต้องแก้ไขมันได้หรือเปล่า?
ขอบคุณ!