From 3ff4935740b569fe79994a74fe437bac0b979854 Mon Sep 17 00:00:00 2001 From: John Freeman Date: Sun, 7 Oct 2018 13:47:22 -0500 Subject: [PATCH 1/4] Add countTimers method Closes #213 --- src/lolex-src.js | 5 ++++- test/lolex-test.js | 16 ++++++++++++++++ 2 files changed, 20 insertions(+), 1 deletion(-) diff --git a/src/lolex-src.js b/src/lolex-src.js index 7b9999c9..ee5226aa 100644 --- a/src/lolex-src.js +++ b/src/lolex-src.js @@ -257,7 +257,6 @@ function withGlobal(_global) { return timer.id; } - /* eslint consistent-return: "off" */ function compareTimers(a, b) { // Sort first by absolute timing @@ -574,6 +573,10 @@ function withGlobal(_global) { return clearTimer(clock, timerId, "Immediate"); }; + clock.countTimers = function countTimers() { + return Object.keys(clock.timers).length; + }; + clock.requestAnimationFrame = function requestAnimationFrame(func) { var result = addTimer(clock, { func: func, diff --git a/test/lolex-test.js b/test/lolex-test.js index a8097839..a068cc92 100644 --- a/test/lolex-test.js +++ b/test/lolex-test.js @@ -432,6 +432,22 @@ describe("lolex", function () { }); + describe("countTimers", function () { + + beforeEach(function () { + this.clock = lolex.createClock(); + }); + + it("counts remaining timers", function () { + this.clock.setTimeout(NOOP, 100); + this.clock.setTimeout(NOOP, 200); + this.clock.setTimeout(NOOP, 300); + this.clock.tick(150); + assert.equals(this.clock.countTimers(), 2); + }); + + }); + describe("tick", function () { beforeEach(function () { From a5e56bbe7ebf3e1cc0e2762de3ba6e98107a9f9b Mon Sep 17 00:00:00 2001 From: John Freeman Date: Sun, 7 Oct 2018 13:54:00 -0500 Subject: [PATCH 2/4] Handle fresh clock --- src/lolex-src.js | 2 +- test/lolex-test.js | 5 +++++ 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/src/lolex-src.js b/src/lolex-src.js index ee5226aa..9ff3fedb 100644 --- a/src/lolex-src.js +++ b/src/lolex-src.js @@ -574,7 +574,7 @@ function withGlobal(_global) { }; clock.countTimers = function countTimers() { - return Object.keys(clock.timers).length; + return Object.keys(clock.timers || {}).length; }; clock.requestAnimationFrame = function requestAnimationFrame(func) { diff --git a/test/lolex-test.js b/test/lolex-test.js index a068cc92..a5db30f4 100644 --- a/test/lolex-test.js +++ b/test/lolex-test.js @@ -438,6 +438,11 @@ describe("lolex", function () { this.clock = lolex.createClock(); }); + it("return zero for a fresh clock", function () { + assert.equals(this.clock.countTimers(), 0); + }); + + it("counts remaining timers", function () { this.clock.setTimeout(NOOP, 100); this.clock.setTimeout(NOOP, 200); From 6b5f51f47a8cc4d9c76aa79cebdf5a4df70cdcf6 Mon Sep 17 00:00:00 2001 From: John Freeman Date: Sun, 7 Oct 2018 13:56:59 -0500 Subject: [PATCH 3/4] Add documentation for countTimers --- Readme.md | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/Readme.md b/Readme.md index 020781be..a4232bd2 100644 --- a/Readme.md +++ b/Readme.md @@ -200,6 +200,11 @@ available in both browser & node environments. Cancels the callback scheduled by the provided id. +### `clock.countTimers()` + +Returns the number of waiting timers. This can be used to assert that a test +finishes without leaking any timers. + ### `clock.hrtime(prevTime?)` Only available in Node.js, mimicks process.hrtime(). From 16769a1f5dbb5f9cfeac806428c314a1d32e13e3 Mon Sep 17 00:00:00 2001 From: John Freeman Date: Sun, 7 Oct 2018 14:56:33 -0500 Subject: [PATCH 4/4] Remove extra empty line --- test/lolex-test.js | 1 - 1 file changed, 1 deletion(-) diff --git a/test/lolex-test.js b/test/lolex-test.js index a5db30f4..6a390a41 100644 --- a/test/lolex-test.js +++ b/test/lolex-test.js @@ -442,7 +442,6 @@ describe("lolex", function () { assert.equals(this.clock.countTimers(), 0); }); - it("counts remaining timers", function () { this.clock.setTimeout(NOOP, 100); this.clock.setTimeout(NOOP, 200);