Click here to Skip to main content
Click here to Skip to main content

Tagged as

Go to top

Mock the window.setTimeout in a Jasmine test to avoid waiting

, 21 Aug 2014
Rate this:
Please Sign up or sign in to vote.
Originally posted on: http://geekswithblogs.net/Aligned/archive/2014/08/21/mock-the-window.settimeout-in-a-jasmine-test-to-avoid-waiting.aspxJasmine 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

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:

<span class="kwrd" style="color: #0000ff">var</span> test = <span class="kwrd" style="color: #0000ff">function</span>(){
        <span class="kwrd" style="color: #0000ff">var</span> self = <span class="kwrd" style="color: #0000ff">this</span>;
        self.timeoutWasCalled = <span class="kwrd" style="color: #0000ff">false</span>;
        self.testWithTimeout = <span class="kwrd" style="color: #0000ff">function</span>(){
           window.setTimeout(<span class="kwrd" style="color: #0000ff">function</span>(){
             self.timeoutWasCalled = <span class="kwrd" style="color: #0000ff">true</span>;
           }, 6000);
        };
};

Here’s my testing code:

<span class="kwrd" style="color: #0000ff">var</span> realWindowSetTimeout = window.setTimeout;
describe(<span class="str" style="color: #006080">'test a method that uses setTimeout'</span>, <span class="kwrd" style="color: #0000ff">function</span>(){
    <span class="kwrd" style="color: #0000ff">var</span> testObject;
    beforeEach(<span class="kwrd" style="color: #0000ff">function</span> () {
        <span class="rem" style="color: #008000">// force setTimeout to be called right away, no matter what time they specify</span>
        jasmine.getGlobal().setTimeout = <span class="kwrd" style="color: #0000ff">function</span> (funcToCall, millis) {
            funcToCall();
        };
        testObject = <span class="kwrd" style="color: #0000ff">new</span> test();
    });
    afterEach(<span class="kwrd" style="color: #0000ff">function</span>() {
        jasmine.getGlobal().setTimeout = realWindowSetTimeout;
    });
    it(<span class="str" style="color: #006080">'should call the method right away'</span>, <span class="kwrd" style="color: #0000ff">function</span>(){
       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)

Share

About the Author

Aligned
Web Developer
United States United States
No Biography provided

Comments and Discussions

 
-- There are no messages in this forum --
| Advertise | Privacy | Mobile
Web04 | 2.8.140905.1 | Last Updated 22 Aug 2014
Article Copyright 2014 by Aligned
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid