Skip to content

Commit

Permalink
Make timers work when Date has been stubbed or modified.
Browse files Browse the repository at this point in the history
  • Loading branch information
austinbv committed Jul 14, 2012
1 parent db59c84 commit 53bf5c8
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 0 deletions.
3 changes: 3 additions & 0 deletions lib/timers.js
Expand Up @@ -26,6 +26,9 @@ var assert = require('assert').ok;
// Timeout values > TIMEOUT_MAX are set to 1.
var TIMEOUT_MAX = 2147483647; // 2^31-1

//Hold reference to the global date so if stubbed the times still work
var Date = this.Date;

This comment has been minimized.

Copy link
@isaacs

isaacs Jul 14, 2012

Shouldn't this be global.Date? (In node modules this === exports.)


var debug;
if (process.env.NODE_DEBUG && /timer/.test(process.env.NODE_DEBUG)) {
debug = function() { require('util').error.apply(this, arguments); };
Expand Down
9 changes: 9 additions & 0 deletions test/pummel/test-timers.js
Expand Up @@ -44,6 +44,15 @@ setTimeout(function() {
setTimeout_called = true;
}, 1000);

assert.doesNotThrow(function() {
var copy = Date;
Date = 'Not a Date';
interval_count = 0;
setInterval(function() { clearInterval(this); }, 100);
setTimeout(function() { return true; }, 100);
Date = copy;
}, TypeError);

// this timer shouldn't execute
var id = setTimeout(function() { assert.equal(true, false); }, 500);
clearTimeout(id);
Expand Down

0 comments on commit 53bf5c8

Please sign in to comment.