diff --git a/src/fake-timers-src.js b/src/fake-timers-src.js index 660fbfa3..ddcb245f 100644 --- a/src/fake-timers-src.js +++ b/src/fake-timers-src.js @@ -585,12 +585,18 @@ function withGlobal(_global) { if (addTimerReturnsObject) { const res = { + refed: true, ref: function () { + this.refed = true; return res; }, unref: function () { + this.refed = false; return res; }, + hasRef: function () { + return this.refed; + }, refresh: function () { clearTimeout(timer.id); const args = [timer.func, timer.delay].concat(timer.args); diff --git a/test/fake-timers-test.js b/test/fake-timers-test.js index 103ec784..2dd5fc3a 100644 --- a/test/fake-timers-test.js +++ b/test/fake-timers-test.js @@ -69,7 +69,6 @@ describe("issue #59", function () { clock.uninstall(); }); }); - describe("issue #73", function () { it("should install with date object", function () { const date = new Date("2015-09-25"); @@ -4834,6 +4833,7 @@ describe("#368 - timeout.refresh setTimeout arguments", function () { this.skip(); } }); + it("should forward arguments passed to setTimeout", function () { const clock = FakeTimers.install(); const stub = sinon.stub(); @@ -5234,3 +5234,56 @@ describe("loop limit stack trace", function () { }); }); }); + +describe("Node Timer: ref(), unref(),hasRef()", function () { + let clock; + + before(function () { + if (!addTimerReturnsObject) { + this.skip(); + } + clock = FakeTimers.install(); + }); + + afterEach(function () { + clock.uninstall(); + }); + + it("should return the ref status as true after initiation", function () { + const stub = sinon.stub(); + const refStatusForTimeout = clock.setTimeout(stub, 0).hasRef(); + const refStatusForInterval = clock.setInterval(stub, 0).hasRef(); + assert.isTrue(refStatusForTimeout); + assert.isTrue(refStatusForInterval); + clock.uninstall(); + }); + + it("should return the ref status as false after using unref", function () { + const stub = sinon.stub(); + const refStatusForTimeout = clock.setTimeout(stub, 0).unref().hasRef(); + const refStatusForInterval = clock + .setInterval(stub, 0) + .unref() + .hasRef(); + assert.isFalse(refStatusForInterval); + assert.isFalse(refStatusForTimeout); + clock.uninstall(); + }); + + it("should return the ref status as true after using unref and then ref ", function () { + const stub = sinon.stub(); + const refStatusForTimeout = clock + .setTimeout(stub, 0) + .unref() + .ref() + .hasRef(); + const refStatusForInterval = clock + .setInterval(stub, 0) + .unref() + .ref() + .hasRef(); + assert.isTrue(refStatusForInterval); + assert.isTrue(refStatusForTimeout); + clock.uninstall(); + }); +});