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 #1801: spied fakeTimers are not restored correctly #1802

Merged

Conversation

ifrost
Copy link
Contributor

@ifrost ifrost commented May 21, 2018

Purpose (TL;DR) - mandatory

When a fakeTimer is spied, sandbox.restore()/sinon.restore() does not restore setTimeout to the original function.

Background (Problem in detail)

See #1801

When sandbox.useFakeTimers() is called, the original setTimeout and other methods are replaced with fake methods. When such fake method is spied it's replaced again with another fake. To make sure that sandbox.restore() reverts setTimeout and other methods to the original state these fakes should be restored in reverse order to make sure each fake is restored to its predecessor when calling fake.restore().

What is more the default sandbox doesn't use sandbox.useFakeTimers(see also #1775) and sandbox.clock (see #1780). To make the fix work in default sandbox it should pass useFakeTimers through to the sandbox method. Using sandbox.clock in the default sandbox added for consistency.

How to verify - mandatory

  1. Check out this branch
  2. npm install
  3. npm test

ig-pj added 4 commits May 21, 2018 11:50
When sandbox.useFakeTimers() is called, the original setTimeout and
other methods are replaced with fake methods. When such fake method is
spied it's replaced again with another fake. To make sure that
sandbox.restore() reverts setTimeout and other methods to the original
state these fakes should be restored in reverse order to make sure each
fake is restored to its predecessor when calling fake.restore().
This is to allow restoring spied fakeTimers in default sandbox.
Copy link
Contributor

@fatso83 fatso83 left a comment

Choose a reason for hiding this comment

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

LGTM!

@@ -24,11 +24,8 @@ var apiMethods = {
setFormatter: format.setFormatter,

// fake timers
useFakeTimers: fakeTimers.useFakeTimers,
Copy link
Contributor

Choose a reason for hiding this comment

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

So these lines that were hanging around from earlier versions essentially shadowed the new sandbox functions on the prototype. Good find!

@fatso83 fatso83 merged commit 5ca48d3 into sinonjs:master May 24, 2018
@fatso83
Copy link
Contributor

fatso83 commented May 24, 2018

Thanks for the fix and the work put in! Out as 5.0.10

@mroderick
Copy link
Member

Thank you for the contribution @ifrost 💯

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants