React Testing setInterval with jest

Viewed 31

I am trying to test a component which saves a piece of information locally every 10 seconds. I am setting the interval in the componentDidMount of the component.

class Example extends React.Component{
    this.interval;
    
    componentDidMound(){
        this.interval = setInterval(() => {
            this.saveDataLocally();
        }, 10000);
    }

    componentWillUnmount(){
        clearInterval(this.interval);
    }

}

The issue I am facing here is that when I try to run the test cases related to this component which are a lot, the setInterval seems to be called every 10 seconds and the running time has increased and it never ends running.

How do I make sure that the setInterval does not interfere while running the other test cases of the component?

0 Answers
Related