Skip to content

Commit

Permalink
Merge pull request #193 from icebob/master
Browse files Browse the repository at this point in the history
Fixes ref() & unref() return value on NodeJS
  • Loading branch information
fatso83 authored Jun 1, 2018
2 parents 8a30ce6 + 8fa1bce commit ef9332b
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 3 deletions.
8 changes: 5 additions & 3 deletions src/lolex-src.js
Original file line number Diff line number Diff line change
Expand Up @@ -245,11 +245,13 @@ 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; },
refresh: function () { return res; }
};
return res;
}

return timer.id;
Expand Down
36 changes: 36 additions & 0 deletions test/lolex-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1750,6 +1750,42 @@ 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("global fake setTimeout().refresh() should return timer", function () {
this.clock = lolex.install();
var stub = sinon.stub();

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

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

0 comments on commit ef9332b

Please sign in to comment.