Click here to Skip to main content
15,896,111 members
Articles / All Topics

Mock the window.setTimeout in a Jasmine Test to Avoid Waiting

Rate me:
Please Sign up or sign in to vote.
5.00/5 (1 vote)
21 Aug 2014CPOL 7.8K  
Mock the window.setTimeout in a Jasmine test to avoid waiting

Originally posted on http://geekswithblogs.net/Aligned/archive/2014/08/21/mock-the-window.settimeout-in-a-jasmine-test-to-avoid-waiting.aspx.

Jasmine has a clock mocking feature, but I was unable to make it work in a function that I’m calling and want to test. The example only shows using clock for a setTimeout in the spec tests and I couldn’t find a good example. Here is my current and slightly limited approach.

If we have a method we want to test:

JavaScript
var test = function(){
        var self = this;
        self.timeoutWasCalled = false;
        self.testWithTimeout = function(){
           window.setTimeout(function(){
             self.timeoutWasCalled = true;
           }, 6000);
        };
};

Here’s my testing code:

C#
var realWindowSetTimeout = window.setTimeout;
describe('test a method that uses setTimeout', function(){
    var testObject;
    beforeEach(function () {
        // force setTimeout to be called right away, no matter what time they specify
        jasmine.getGlobal().setTimeout = function (funcToCall, millis) {
            funcToCall();
        };
        testObject = new test();
    });
    afterEach(function() {
        jasmine.getGlobal().setTimeout = realWindowSetTimeout;
    });
    it('should call the method right away', function(){
       testObject.testWithTimeout();
       expect(testObject.timeoutWasCalled).toBeTruthy();
    });
});

I got a good pointer from Andreas in this StackOverflow question.

This would also work for window.setInterval.

Other possible approaches:

  • Create a wrapper module of setTimeout and setInterval methods that can be mocked. This can be mocked with RequireJS or passed into the constructor.
  • Pass the window.setTimeout function into the method (this could get messy).

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Web Developer
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
-- There are no messages in this forum --