Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(timer.refresh): should just change callAt #425

Merged
merged 6 commits into from
Mar 3, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 8 additions & 3 deletions src/fake-timers-src.js
Original file line number Diff line number Diff line change
Expand Up @@ -598,9 +598,14 @@ function withGlobal(_global) {
return this.refed;
},
refresh: function () {
clearTimeout(timer.id);
const args = [timer.func, timer.delay].concat(timer.args);
return setTimeout.apply(null, args);
timer.callAt =
clock.now +
(parseInt(timer.delay) || (clock.duringTick ? 1 : 0));

// it _might_ have been removed, but if not the assignment is perfectly fine
clock.timers[timer.id] = timer;

return res;
},
[Symbol.toPrimitive]: function () {
return timer.id;
Expand Down
24 changes: 15 additions & 9 deletions test/fake-timers-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3607,16 +3607,16 @@ describe("FakeTimers", function () {
}
});

it("global fake setTimeout().refresh() should return timer", function () {
it("global fake setTimeout().refresh() should return same timer", function () {
this.clock = FakeTimers.install();
const stub = sinon.stub();

if (typeof setTimeout(NOOP, 0) === "object") {
const to = setTimeout(stub, 1000).refresh();
assert.isNumber(Number(to));
assert.isFunction(to.ref);
assert.isFunction(to.refresh);
const timeout = setTimeout(stub, 1000);
const to = timeout.refresh();
assert(timeout === to);
}
this.clock.uninstall();
});

it("replaces global clearTimeout", function () {
Expand Down Expand Up @@ -4834,7 +4834,7 @@ describe("#368 - timeout.refresh setTimeout arguments", function () {
}
});

it("should forward arguments passed to setTimeout", function () {
it("should forward arguments passed to setTimeout", function () {
const clock = FakeTimers.install();
const stub = sinon.stub();

Expand Down Expand Up @@ -4863,13 +4863,19 @@ describe("#187 - Support timeout.refresh in node environments", function () {
clock.uninstall();
});

it("assigns a new id to the refreshed timer", function () {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is plain wrong, as mentioned in the OP

it("only calls stub once if not fired at time of refresh", function () {
const clock = FakeTimers.install();
const stub = sinon.stub();

if (typeof setTimeout(NOOP, 0) === "object") {
const t = setTimeout(stub, 1000);
const t2 = t.refresh();
refute.same(Number(t), Number(t2));
clock.tick(999);
assert(stub.notCalled);
t.refresh();
clock.tick(999);
assert(stub.notCalled);
clock.tick(1);
assert(stub.calledOnce);
}
clock.uninstall();
});
Expand Down