Skip to content

Allow sinon.useFakeTimers() to modify setTimeout() and clearTimeout() after module loads#45

Merged
sindresorhus merged 2 commits intosindresorhus:masterfrom
Torgen:master
Jun 12, 2019
Merged

Allow sinon.useFakeTimers() to modify setTimeout() and clearTimeout() after module loads#45
sindresorhus merged 2 commits intosindresorhus:masterfrom
Torgen:master

Conversation

@Torgen
Copy link
Copy Markdown
Contributor

@Torgen Torgen commented Jun 7, 2019

If setTimeout and clearTimeout aren't explicitly set in the createDelay() call, bind their values when the promise is created. This allows sinon.useFakeTimers() to work while still allowing other implementations of those functions to be passed to createDelay(). Previously the default setTimeout and clearTimeout were set aside when the module was loaded, meaning default delay and delay.reject didn't notice if they were later stubbed out.
Resolves the issue I raised in the comments for #39.

If `setTimeout` and `clearTimeout` aren't explicitly set in the `createDelay()` call, bind their values when the promise is created. This allows `sinon.useFakeTimers()` to work while still allowing other implementations of those functions to be passed to `createDelay()`. Previously the default `setTimeout` and `clearTimeout` were set aside when the module was loaded, meaning default `delay` and `delay.reject` didn't notice if they were later stubbed out.
index.js Outdated
let timeoutId;
let settle;
let rejectFn;
const c = clear || clearTimeout;
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

Needs a more descriptive variable name than c.

@sindresorhus
Copy link
Copy Markdown
Owner

The PR title should describe what it fixes, not what it contains.

@Torgen Torgen changed the title Late binding of default setTimeout and clearTimeout Allow sinon.useFakeTimers() to modify setTimeout() and clearTimeout() after module loads Jun 12, 2019
@sindresorhus sindresorhus merged commit 1316fd7 into sindresorhus:master Jun 12, 2019
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.

2 participants