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

Fixes ref() & unref() return value on NodeJS #193

Merged
merged 2 commits into from
Jun 1, 2018
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions src/lolex-src.js
Original file line number Diff line number Diff line change
Expand Up @@ -245,11 +245,12 @@ function withGlobal(_global) {
clock.timers[timer.id] = timer;

if (addTimerReturnsObject) {
return {
var res = {
id: timer.id,
ref: NOOP,
unref: NOOP
ref: function () { return res; },
unref: function () { return res; }
Copy link
Member

Choose a reason for hiding this comment

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

Can you add .refresh here?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

What is the .refresh?

Copy link
Member

Choose a reason for hiding this comment

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

It's a new API Node recently added in nodejs/node#20298 that landed in 10.2.0

Copy link
Member

Choose a reason for hiding this comment

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

If you don't feel like it I'll land and can release when I get back from the Node summit/JSConf next week.

Just saying I would appreciate it if you did it while touching that code - this will land regardless - no pressure.

Already very happy that you decided to contribute to the project :)

Copy link
Contributor Author

@icebob icebob Jun 1, 2018

Choose a reason for hiding this comment

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

Ohh, I've not known about it. No problem, I'm adding it. :)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Or I need to add some "refresh" logic as well?

Copy link
Contributor

@fatso83 fatso83 Jun 1, 2018

Choose a reason for hiding this comment

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

I think adding the whole logic is a bit much in this (essentially unrelated PR), but adding a NOOP function returning the timeout should be sufficient. Someone else can supply a full PR.

};
return res;
}

return timer.id;
Expand Down
24 changes: 24 additions & 0 deletions test/lolex-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1750,6 +1750,30 @@ describe("lolex", function () {
}
});

it("global fake setTimeout().ref() should return timer", function () {
this.clock = lolex.install();
var stub = sinon.stub();

if (typeof (setTimeout(NOOP, 0)) === "object") {
var to = setTimeout(stub, 1000).ref();
assert.isNumber(to.id);
assert.isFunction(to.ref);
assert.isFunction(to.unref);
}
});

it("global fake setTimeout().unref() should return timer", function () {
this.clock = lolex.install();
var stub = sinon.stub();

if (typeof (setTimeout(NOOP, 0)) === "object") {
var to = setTimeout(stub, 1000).unref();
assert.isNumber(to.id);
assert.isFunction(to.ref);
assert.isFunction(to.unref);
}
});

it("replaces global clearTimeout", function () {
this.clock = lolex.install();
var stub = sinon.stub();
Expand Down