테스트 기능을 가지고 약속하고 setTimeout,왜 그것은 타이밍니까?

0

질문

나 테스트하기 위해 노력하고있는 기능 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 ...

어떤 아이디어로 이 일어나는 이유는 무엇입니까? 또 그것을 해결하는 방법?

감사합니다!

javascript jestjs settimeout timeout
2021-11-24 01:41:50
1

최고의 응답

1

여기에는 솔루션을 가까이 가서 무엇이겠습니다. 중요한 것은,할 수 없습니다 await 해상도의 약속을 사용이 가 타이머 또는 그것은 결코 해결합니다. 대신,당신을 호출 할 수 있습당의 반환 값 sleep 기능 변수,그 후 실행하여 타이머,다음을 기다리고 변수입니다.

또한 조정의 expect 문에 대한 제한 시간이 스파이기 때문에 그것은 두 개의 인수를 사용합니다. 마지막으로,이를 제거하는 expectationt sleep 라진 기간이기 때문에 당신은 말 그대로 하고 있는 테스트,그래서 그것은 보이지 않는 가치를 만들 것을 주장하고 있습니다.

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");

  const sleepFn = sleep(+SLEEP_DURATION);

  jest.runAllTimers();

  // Now that we ran timers we can await the promise resolving 
  await sleepFn;

  // timeout takes two arguments
  expect(timeoutSpy).toHaveBeenCalledWith(
    expect.any(Function),
    +SLEEP_DURATION
  );
  expect(console.log).toHaveBeenCalledWith("sleep end");
});
2021-11-24 02:03:50

에 대한 감사의 유익한 응답,이번이 처음 시간을 사용하여 이러한 농담으로 타이머 그래서 내가 확실히 공부 할 수있다!
ffx292

다른 언어로

이 페이지는 다른 언어로되어 있습니다

Русский
..................................................................................................................
Italiano
..................................................................................................................
Polski
..................................................................................................................
Română
..................................................................................................................
हिन्दी
..................................................................................................................
Français
..................................................................................................................
Türk
..................................................................................................................
Česk
..................................................................................................................
Português
..................................................................................................................
ไทย
..................................................................................................................
中文
..................................................................................................................
Español
..................................................................................................................
Slovenský
..................................................................................................................