From a13afb9a9740320ed5d6dffa1cf82a76f3bfc791 Mon Sep 17 00:00:00 2001 From: Icebob Date: Fri, 1 Jun 2018 14:04:17 +0200 Subject: [PATCH 1/2] fixes #191 - ref(), unref() return value on NodeJS --- src/lolex-src.js | 7 ++++--- test/lolex-test.js | 24 ++++++++++++++++++++++++ 2 files changed, 28 insertions(+), 3 deletions(-) diff --git a/src/lolex-src.js b/src/lolex-src.js index fa147b99..0aa177f6 100644 --- a/src/lolex-src.js +++ b/src/lolex-src.js @@ -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; } }; + return res; } return timer.id; diff --git a/test/lolex-test.js b/test/lolex-test.js index ec642e1e..630b906b 100644 --- a/test/lolex-test.js +++ b/test/lolex-test.js @@ -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(); From 8fa1bceacac08d4b438207a42939a386e8ade9d6 Mon Sep 17 00:00:00 2001 From: Icebob Date: Fri, 1 Jun 2018 15:23:55 +0200 Subject: [PATCH 2/2] add `.refresh` to timer object. --- src/lolex-src.js | 3 ++- test/lolex-test.js | 12 ++++++++++++ 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/src/lolex-src.js b/src/lolex-src.js index 0aa177f6..53982547 100644 --- a/src/lolex-src.js +++ b/src/lolex-src.js @@ -248,7 +248,8 @@ function withGlobal(_global) { var res = { id: timer.id, ref: function () { return res; }, - unref: function () { return res; } + unref: function () { return res; }, + refresh: function () { return res; } }; return res; } diff --git a/test/lolex-test.js b/test/lolex-test.js index 630b906b..bca74ce8 100644 --- a/test/lolex-test.js +++ b/test/lolex-test.js @@ -1774,6 +1774,18 @@ describe("lolex", function () { } }); + 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();