title: Sleep in Jasmine
body: <p>Usecase:
  For my angularJS(1.6.4) application, I am writing unit test in jasmine.</p>
  
  <p>In my test case, I call a poll() function which keeps calling another function CheckTaskStatus() repeatedly using $interval. I am spy-ing CheckTaskStatus() and want to check that it has been called a certain number of times. 
  So after calling poll(), I want to be able to wait for some time and then check the the number of times CheckTaskStatus() is called using expect().</p>
  
  <p>Problem:
  I have been unable to find a way to make jasmine wait after call to poll() and before expect(). </p>
  
  <p>After poll() I have tried using below options, but those do not cause jasmine to sleep:</p>
  
  <ul>
  <li>$timeout</li>
  <li>settimeout</li>
  <li>async function which calls await on function returning promise. 
  <strong>Service</strong>
  import * as angular from 'angular';</li>
  </ul>
  
  <p>export class Polling {
      public static $inject = ['$q', '$http', '$interval'];</p>
  
  <pre><code>constructor(private $q: ng.IQService, private $http, private $interval) {}
  
  public poll(interval: number, pollFn: () =&gt; ng.IPromise&lt;any&gt;, until?: (any) =&gt; boolean, cancel?: ng.IPromise&lt;any&gt;) {
      let intervalPromise = null;
      const done = this.$q.defer();
  
      const intervalFn = () =&gt; {
          pollFn().then((pollFnResult) =&gt; {
              if (until &amp;&amp; until(pollFnResult)) {
                  this.$interval.cancel(intervalPromise);
                  done.resolve();
              }
          }).catch(() =&gt; {});  
      };
  
      // Set up an interval to execute the pollFunction
      intervalPromise = this.$interval(intervalFn, interval);
      intervalPromise.catch(() =&gt; {});
  
      intervalFn();
  
      if (cancel) {
          cancel.then(() =&gt; {
              this.$interval.cancel(intervalPromise);
              done.resolve();
          })
              .catch(() =&gt; {}); 
      }
  
      return done.promise;
  }
  </code></pre>
  
  <p>}</p>
  
  <p>export default angular.module('myPolling', [])
      .service('polling', Polling).name;</p>
  
  <p><strong>Jasmine test</strong></p>
  
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-html lang-html prettyprint-override"><code>fdescribe('myPolling module tests', () =&gt; {
      'use strict';
      let $rootScope,
          $q: ng.IQService,
          $http,
          $interval,
          $timeout;
  
      beforeEach(mockModule('myPolling'));
      beforeEach(() =&gt; {
          jasmine.clock().install();
      });
      beforeEach(inject((_$rootScope_, _$q_, _$http_, _$interval_, _polling_, _$timeout_) =&gt; {
          this.$rootScope = _$rootScope_;
          this.$q = _$q_;
          this.$http = _$http_;
          this.$interval = _$interval_;
          this.polling = _polling_;
          $timeout = _$timeout_;
  
  
      }));
  
      function sleep(ms) {
          return new Promise(resolve =&gt; setTimeout(resolve, ms));
      }
  
      async function SleepForASecond() {
          await sleep(1050);
      }
  
      it('blah', () =&gt; {
  
          let pollCount = 0;
          let pollObj = {
              pollFn: () =&gt; {
                  pollCount++;
                  return this.$q.when(pollCount);
              }
          };
           // jasmine.createSpy('pollFn');
          spyOn(pollObj, 'pollFn').and.callThrough();
  
          let cancel = this.$q.defer();
          this.polling.poll(100, pollObj.pollFn, () =&gt; false, cancel.promise);
    
    // Need a mechanism to wait for a second.
    // SleepForASecond().then(() =&gt; { cancel.resolve(); });
    
          expect(pollObj.pollFn).toHaveBeenCalledTimes(10);
  
      });
  
  });</code></pre>
  </div>
  </div>
  </p>