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 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
6 changes: 5 additions & 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 Expand Up @@ -309,6 +309,10 @@ Implements the `now` method of the [`Performance`](https://developer.mozilla.org

In order to support creating clocks based on separate or sandboxed environments (such as JSDOM), FakeTimers exports a factory method which takes single argument `global`, which it inspects to figure out what to mock and what features to support. When invoking this function with a global, you will get back an object with `timers`, `createClock` and `install` - same as the regular FakeTimers exports only based on the passed in global instead of the global environment.

## Promises and fake time

If you use a Promise library like Bluebird, note that you should either call `clock.runMicrotasks()` or make sure to _not_ mock `nextTick`.

## Running tests

FakeTimers has a comprehensive test suite. If you're thinking of contributing bug
Expand Down
9 changes: 1 addition & 8 deletions src/fake-timers-src.js
Expand Up @@ -1259,14 +1259,7 @@ function withGlobal(_global) {
return uninstall(clock, config);
};

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 = config.toFake || keys(timers);
Copy link
Member Author

Choose a reason for hiding this comment

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

another technically breaking change I guess - if people passed toFake: [] before, we faked everything. Seems like a bug fix to me to respect it, but maybe not 😀 If not, should just revert the last commit

Copy link
Member

Choose a reason for hiding this comment

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

Yeah I think this and the other one should be a major version if we land them.

Copy link
Member Author

Choose a reason for hiding this comment

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

Can open a separate PR for it?

Copy link
Contributor

Choose a reason for hiding this comment

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

Can open a separate PR for it?

Yes


for (i = 0, l = clock.methods.length; i < l; i++) {
if (clock.methods[i] === "hrtime") {
Expand Down
17 changes: 2 additions & 15 deletions test/fake-timers-test.js
Expand Up @@ -4494,8 +4494,8 @@ describe("FakeTimers", function() {
assert(typeof timers[0].id !== "undefined");
});

it("passes arguments when installed - GitHub#122", function() {
var clock = FakeTimers.install({ toFake: ["nextTick"] });
it("nextTick passes arguments", function() {
var clock = FakeTimers.install();
var called = false;
process.nextTick(function(value) {
called = value;
Expand All @@ -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