Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to write a test case for a setInterval() function

I have a timer and assume a specific function will be executed when the counter is counting to 3.

var a_interval_function = function(){
    var counter = 1;
    var interval = setInterval(function(){
        if(counter === 5){
            clearInterval(interval);
        }

        // run the function when the counter is 3
        if(counter === 3){
            a_function_should_be_runned();
        }   

        counter++;
    }, 500);

    return interval;
}

However, I don't know how to establish a valid test case for testing the counter as well as the timing when the function is executed. Does anyone know how to do it? Something like the following:

// and some test case like this
it('a timer test', function(done){
    var interval = a_interval_function();
    expect(a_function_should_be_runned.state).to.equal({
        name: 'runned',
        counter: 3,
        time: 300,
    });
});

Thanks.

like image 949
Tony Hsieh Avatar asked Dec 16 '15 10:12

Tony Hsieh


People also ask

How do you write a test case for setInterval?

Something like the following: // and some test case like this it('a timer test', function(done){ var interval = a_interval_function(); expect(a_function_should_be_runned. state).to. equal({ name: 'runned', counter: 3, time: 300, }); });

How do you test Jasmine setInterval?

const x = setInterval(() => { const countdown = getElementById('countdownWrapper'); const systemTime = ... const now = new Date(). getTime(); const endTime = systemTime - now; countdown.

How do I write a test case for setTimeout in Jasmine?

Jasmine supports testing async code. We can test async code with: describe("Using callbacks", function () { beforeEach(function (done) { setTimeout(function () { value = 0; done(); }, 1); }); it("supports sequential execution of async code", function (done) { value++; expect(value). toBeGreaterThan(0); done(); }); });


1 Answers

Perhaps you can use sinon.useFakeTimers().

For example:

var sinon  = require('sinon');
var expect = require('chai').expect;

var a_function_should_be_runned = sinon.spy();

var a_interval_function = function(){
  var counter = 1;
  var interval = setInterval(function(){
    if(counter === 5){
      clearInterval(interval);
    }

    // run the function when the counter is 3
    if(counter === 3){
      a_function_should_be_runned();
    }   

    counter++;
  }, 500);

  return interval;
}

describe('timer tests', function() {

  before(function() {
    this.clock = sinon.useFakeTimers();
  });

  after(function() {
    this.clock.restore();
  });

  it('a timer test', function() {
    var interval = a_interval_function();

    // At time 0, we don't expect the function to have been called.
    expect(a_function_should_be_runned.called).to.be.false;

    // Advance clock 500ms.
    this.clock.tick(500);
    expect(a_function_should_be_runned.called).to.be.false;

    // Advance clock again (1s since start)
    this.clock.tick(500);
    expect(a_function_should_be_runned.called).to.be.false;

    // Advance clock again (1.5s since start). This should
    // trigger the call to `a_function_should_be_runned`.
    this.clock.tick(500);
    expect(a_function_should_be_runned.called).to.be.true;
  });
});
like image 85
robertklep Avatar answered Oct 23 '22 03:10

robertklep