Skip to content

Commit

Permalink
clock.useFakeTimersSafe() - doesn't stub setImmediate
Browse files Browse the repository at this point in the history
  • Loading branch information
Edan Schwartz committed Oct 24, 2016
1 parent 0255303 commit 56767e5
Showing 1 changed file with 17 additions and 0 deletions.
17 changes: 17 additions & 0 deletions lib/sinon-clock-singleton.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,23 @@ const clock = {
clock.restore();
clockInstance = sinon.useFakeTimers.apply(sinon, arguments);
},
/**
* Creates a sinon clock instance,
* without stubbing `setImmediate`
*
* See
* https://github.com/sinonjs/sinon/issues/960
*/
useFakeTimersSafe: function() {
const args = [].slice.call(arguments, 0);
const time = args.length ? args[0] : Date.now();
const stubbedMethods = args.length > 1 ?
// Stub everything except setImmediate
['Date', 'setInterval', 'clearInterval', 'setTimeout', 'clearTimeout'] :
args.slice(1);

clock.useFakeTimers.apply(clock, [time].concat(stubbedMethods))
},
tick: (ms) => {
if (!clockInstance) { clock.useFakeTimers();}
clockInstance.tick(ms);
Expand Down

0 comments on commit 56767e5

Please sign in to comment.