title: How to spyOn global timers like clearInterval or setInterval in jasmine-node tests?
body: <p>I'm writing tests on my server side code which is in nodejs. The tests are written in jasmine 2. I'm running the tests using grunt-jasmine-nodejs.</p>
  
  <p>The server side code uses the timers like setInterval and clearInterval. I want to spy on these timers and see whether these are called.</p>
  
  <p><strong>Server side code  - server.js</strong></p>
  
  <pre><code>exports.socketHandler = function(socket){    
  ...
  getResultsInterval = setInterval(getResults, INITIAL_POLL, onResults);    
  getResultsTimeout = setTimeout(function() {               
                clearInterval(getResultsInterval);          
              }, 1 * 60 * 1000);
  }
  </code></pre>
  
  <p><strong>Test</strong></p>
  
  <pre><code>var server = require("../../server");
  describe('Test the Server', function(){
  
  beforeEach(function(done)){
  ....
  done();
  }
  
   it ('should be able to clear timers', function(done){
  spyOn(global, 'setInterval');
  spyOn(global, 'clearInterval');
  
  server.socketHandler(socket);
  
  expect(global.setInterval).toHaveBeenCalled();
  expect(global.clearInterval).toHaveBeenCalled();
  
  }
  </code></pre>
  
  <p>The tests are failing with the message - </p>
  
  <blockquote>
    <p>Error: Expected spy setInterval to have been called.
    Error: Expected spy clearInterval to have been called.</p>
  </blockquote>
  
  <p>But the global object has the setInterval method.</p>
  
  <p>Any help would be appreciated.</p>
  
  <p>Thanks</p>