r/learnreactjs • • May 11 '22

Question [JEST and Testing Library]: setTimeout callback is not being called in UNIT TESTS even after using fakeTimer and runAllTimers

The implementation is somewhat like this:

// component
const MyComponent = () => {
    const [timer, setTimer] = useState(5);
    useEffect(() => {
        const timeout = setTimeout(() => {
            console.log('***** Timer Ran!!! *********');
            if(timer <= 5 && timer > 0) setTimer(timer - 1);
            else {
                return () => clearTimeout(timeout);
            }
        }, 1000);
    }, [timer]);

    <>
        // some JSX
    </>
};

// test

jest.useFakeTimers(); // at top of file

it('should run code inside useEffect', () => {
    const startBtn = screen.getByTestId('start-btn');
    expect(startBtn).toBeEnabled();
    
    jest.runAllTimers();
});

I can't figure out why the callback passed to setTimeout is not called in UNIT TESTS even after using **jest.runAllTimers**. Am I missing something?

Note: I have tried wrapping it in waitFor and act and it doesn't work.

4 Upvotes

2 comments sorted by

View all comments

Show parent comment

1

u/[deleted] May 11 '22

Thanks for sharing. Hopefully, it solves the issue. Will go through it.