setTimeout only called once time in useEffect with Jest unit test

55 views Asked by At

I have countdown function in useEffect and i want to write unit-test for it:

 [countDown,setCountDown] = useEffect(60)
 useEffect(() => {
    if (countDown !== 0) {
      countdownTimeout = setTimeout(() => {
        setCountDown(countDown - 1);
      }, 1000);
    }
  }, [countDown]);
 it('`countdown', async () => {
      jest.useFakeTimers();

      const { queryByText } = renderOnboardingVerificationScreen();

      expect(queryByText('60')).toBeTruthy();

      await act(async () => jest.advanceTimersByTime(2100));

      expect(queryByText('58')).toBeTruthy();
    });

The result is test case false and countDown value equal 59. I put console log and found that function inside setTimeOut only call one time although i increase timer.

Anyone can help me explain?

0

There are 0 answers