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: fake all supported timers by default #323

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
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
2 changes: 1 addition & 1 deletion README.md
Expand Up @@ -143,7 +143,7 @@ Parameter | Type | Default | Description
--------- | ---- | ------- | ------------
`config.target`| Object | global | installs FakeTimers onto the specified target context
`config.now` | Number/Date | 0 | installs FakeTimers with the specified unix epoch
`config.toFake` | String[] | ["setTimeout", "clearTimeout", "setImmediate", "clearImmediate","setInterval", "clearInterval", "Date", "requestAnimationFrame", "cancelAnimationFrame", "requestIdleCallback", "cancelIdleCallback", "hrtime"] | an array with explicit function names to hijack. *When not set, FakeTimers will automatically fake all methods **except** `nextTick`* e.g., `FakeTimers.install({ toFake: ["setTimeout","nextTick"]})` will fake only `setTimeout` and `nextTick`
`config.toFake` | String[] | ["setTimeout", "clearTimeout", "setImmediate", "clearImmediate","setInterval", "clearInterval", "Date", "requestAnimationFrame", "cancelAnimationFrame", "requestIdleCallback", "cancelIdleCallback", "hrtime"] | an array with explicit function names to hijack. *When not set, FakeTimers will automatically fake all methods* e.g., `FakeTimers.install({ toFake: ["setTimeout","nextTick"]})` will fake only `setTimeout` and `nextTick`
`config.loopLimit` | Number | 1000 | the maximum number of timers that will be run when calling runAll()
`config.shouldAdvanceTime` | Boolean | false | tells FakeTimers to increment mocked time automatically based on the real system time shift (e.g. the mocked time will be incremented by 20ms for every 20ms change in the real system time)
`config.advanceTimeDelta` | Number | 20 | relevant only when using with `shouldAdvanceTime: true`. increment mocked time by `advanceTimeDelta` ms every `advanceTimeDelta` ms change in the real system time.
Expand Down
5 changes: 1 addition & 4 deletions src/fake-timers-src.js
Expand Up @@ -1262,10 +1262,7 @@ function withGlobal(_global) {
clock.methods = config.toFake || [];

if (clock.methods.length === 0) {
// do not fake nextTick by default - GitHub#126
clock.methods = keys(timers).filter(function(key) {
return key !== "nextTick" && key !== "queueMicrotask";
Copy link
Member Author

@SimenB SimenB May 11, 2020

Choose a reason for hiding this comment

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

seems docs were lying 🤷 queueMicrotask was also not mocked by default

});
clock.methods = keys(timers);
}

for (i = 0, l = clock.methods.length; i < l; i++) {
Expand Down
13 changes: 0 additions & 13 deletions test/fake-timers-test.js
Expand Up @@ -4504,19 +4504,6 @@ describe("FakeTimers", function() {
assert(called);
clock.uninstall();
});

it("does not install by default - GitHub#126", function(done) {
Copy link
Member

Choose a reason for hiding this comment

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

Would it be possible to change this test to assert nextTick is mocked if the behavior changes?

Copy link
Member Author

Choose a reason for hiding this comment

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

changed the test above, I think that's fine?

Copy link
Contributor

Choose a reason for hiding this comment

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

changed the test above, I think that's fine?

Yes, this is fine. It's implicitly true, as the code won't work unless nextTick is synchronous.

var clock = FakeTimers.install();
var spy = sinon.spy(clock, "nextTick");
var called = false;
process.nextTick(function(value) {
called = value;
assert(called);
assert(!spy.called);
clock.uninstall();
done();
}, true);
});
});

describe("requestIdleCallback", function() {
Expand Down