From 63677acdd6dccfcf22d816154701d57efa49ce02 Mon Sep 17 00:00:00 2001 From: soferdani Date: Sun, 30 Jan 2022 18:44:26 +0200 Subject: [PATCH 1/4] add hasRef attribute --- src/fake-timers-src.js | 6 ++++++ 1 file changed, 6 insertions(+) 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); From 7db83686d6182443c7df0a800f03eb33fdfb7921 Mon Sep 17 00:00:00 2001 From: soferdani Date: Wed, 2 Feb 2022 21:58:10 +0200 Subject: [PATCH 2/4] add test to the hasRef feature --- test/fake-timers-test.js | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/test/fake-timers-test.js b/test/fake-timers-test.js index 103ec784..210ed372 100644 --- a/test/fake-timers-test.js +++ b/test/fake-timers-test.js @@ -70,6 +70,38 @@ describe("issue #59", function () { }); }); +describe("issue #419", function () { + it("should return the ref status as true after initiation", function () { + const clock = FakeTimers.install(); + 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 clock = FakeTimers.install(); + 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 clock = FakeTimers.install(); + 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(); + }); +}); + describe("issue #73", function () { it("should install with date object", function () { const date = new Date("2015-09-25"); From 984dcb3525415d84fe6b6d379cce29a37c715fec Mon Sep 17 00:00:00 2001 From: Carl-Erik Kopseng Date: Thu, 3 Feb 2022 11:37:56 +0100 Subject: [PATCH 3/4] Run Prettier --- test/fake-timers-test.js | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/test/fake-timers-test.js b/test/fake-timers-test.js index 210ed372..1a31014d 100644 --- a/test/fake-timers-test.js +++ b/test/fake-timers-test.js @@ -85,7 +85,10 @@ describe("issue #419", function () { const clock = FakeTimers.install(); const stub = sinon.stub(); const refStatusForTimeout = clock.setTimeout(stub, 0).unref().hasRef(); - const refStatusForInterval = clock.setInterval(stub, 0).unref().hasRef(); + const refStatusForInterval = clock + .setInterval(stub, 0) + .unref() + .hasRef(); assert.isFalse(refStatusForInterval); assert.isFalse(refStatusForTimeout); clock.uninstall(); @@ -94,8 +97,16 @@ describe("issue #419", function () { it("should return the ref status as true after using unref and then ref ", function () { const clock = FakeTimers.install(); const stub = sinon.stub(); - const refStatusForTimeout = clock.setTimeout(stub, 0).unref().ref().hasRef(); - const refStatusForInterval = clock.setInterval(stub, 0).unref().ref().hasRef(); + 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(); From 45c414aee83e9134b16daf389cea052780b26e51 Mon Sep 17 00:00:00 2001 From: Carl-Erik Kopseng Date: Thu, 3 Feb 2022 12:22:33 +0100 Subject: [PATCH 4/4] Cleanup and add conditional for non-Node environments --- test/fake-timers-test.js | 98 ++++++++++++++++++++++------------------ 1 file changed, 54 insertions(+), 44 deletions(-) diff --git a/test/fake-timers-test.js b/test/fake-timers-test.js index 1a31014d..2dd5fc3a 100644 --- a/test/fake-timers-test.js +++ b/test/fake-timers-test.js @@ -69,50 +69,6 @@ describe("issue #59", function () { clock.uninstall(); }); }); - -describe("issue #419", function () { - it("should return the ref status as true after initiation", function () { - const clock = FakeTimers.install(); - 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 clock = FakeTimers.install(); - 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 clock = FakeTimers.install(); - 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(); - }); -}); - describe("issue #73", function () { it("should install with date object", function () { const date = new Date("2015-09-25"); @@ -4877,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(); @@ -5277,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(); + }); +});