Skip to content

Commit

Permalink
Preserve setTimeout/setInterval Arguments
Browse files Browse the repository at this point in the history
  • Loading branch information
yungsters committed Oct 17, 2014
1 parent bfc8232 commit f37ecb7
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 2 deletions.
18 changes: 16 additions & 2 deletions src/lib/FakeTimers.js
Original file line number Diff line number Diff line change
Expand Up @@ -267,11 +267,18 @@ FakeTimers.prototype._fakeSetInterval = function(callback, intervalDelay) {
intervalDelay = 0;
}

var args = [];
for (var ii = 2, ll = arguments.length; ii < ll; ii++) {
args.push(arguments[ii]);
}

var uuid = this._uuidCounter++;

this._timers[uuid] = {
type: 'interval',
callback: callback,
callback: function() {
return callback.apply(null, args);
},
expiry: this._now + intervalDelay,
interval: intervalDelay
};
Expand All @@ -284,11 +291,18 @@ FakeTimers.prototype._fakeSetTimeout = function(callback, delay) {
delay = 0;
}

var args = [];
for (var ii = 2, ll = arguments.length; ii < ll; ii++) {
args.push(arguments[ii]);
}

var uuid = this._uuidCounter++;

this._timers[uuid] = {
type: 'timeout',
callback: callback,
callback: function() {
return callback.apply(null, args);
},
expiry: this._now + delay,
interval: null
};
Expand Down
13 changes: 13 additions & 0 deletions src/lib/__tests__/FakeTimers-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,19 @@ describe('FakeTimers', function() {
expect(fn.mock.calls.length).toBe(1);
});

it('runs callbacks with arguments after the interval', function() {
var global = {};
var fakeTimers = new FakeTimers(global);

var fn = jest.genMockFn();
global.setTimeout(fn, 0, 'mockArg1', 'mockArg2');

fakeTimers.runAllTimers();
expect(fn.mock.calls).toEqual([
['mockArg1', 'mockArg2']
]);
});

it('doesnt pass the callback to native setTimeout', function() {
var nativeSetTimeout = jest.genMockFn();

Expand Down

0 comments on commit f37ecb7

Please sign in to comment.