Search code examples
reactjsunit-testingtimerjestjssinon

test timers in react, sinon or jest


I have a funtion checkIdleTime, being set to be called periodically.

componentDidMount() {
    var idleCheck = setInterval(this.checkIdleTime.bind(this), authTimeoutSeconds * 1000);
    this.setState({idleCheck: idleCheck});

    document.onkeypress = this.setActive;
}

I want to use the fake timer for test, but can't figure out how, tried sinon and jest.

beforeEach(() => {   
    checkIdleTime = jest.spyOn(PureMain.prototype, 'checkIdleTime');
    wrapper = shallow(
        <PureMain/>
    );

    jest.useFakeTimers();
    //clock = sinon.useFakeTimers();  
});

it('should check the idle time after [authTimeoutSeconds] seconds of inactivity', () => {

    wrapper.instance().componentDidMount();

    var idleCheck_timeout = wrapper.instance().state.idleCheck;
    expect(idleCheck_timeout).not.toEqual(null);
    expect(idleCheck_timeout._idleTimeout).toBe(authTimeoutSeconds * 1000);
    jest.runAllTimers();
    //clock.tick(authTimeoutSeconds * 1000 * 2);
    expect(setInterval).toHaveBeenCalledTimes(1);//not work
    expect(checkIdleTime).toHaveBeenCalledTimes(1);//not work
})

Got error:

Expected mock function to have been called one time, but it was called zero times.

I tried to follow those examples

https://facebook.github.io/jest/docs/en/timer-mocks.html

http://sinonjs.org/releases/v1.17.7/fake-timers/


Can't find a good example on how to spy on setInterval

expect(setInterval.mock.calls.length).toBe(1)

Cannot read property 'calls' of undefined

expect(setInterval).toHaveBeenCalledTimes(1)

value must be a mock function or spy.


Solution

  • Turned out the spy should be declared as:

    setTimeout_spy = jest.spyOn(window, 'setTimeout');