나 테스트하기 위해 노력하고있는 기능 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 ...
어떤 아이디어로 이 일어나는 이유는 무엇입니까? 또 그것을 해결하는 방법?
감사합니다!