Repository: sinonjs/lolex (js)

Issue:
Include ticks in countTimers
<!--
Are you wondering how to do something with Lolex, or something else that is purely
usage related? Please post it to StackOverflow using the `sinon` and `lolex` labels.
This makes it possible for the bigger community to help answer your questions.

The issue tracker is solely meant for posting bugs, feature requests and non-usage related issues.
-->

> We understand you have a problem and are in a hurry, but please provide us with some info to make it much more likely for your issue to be understood, worked on and resolved quickly.


* Lolex version : 4.0.1
* Environment   : Node 10
* Example URL   : N/A
* Other libraries you are using: N/A

**What did you expect to happen?**
See https://github.com/facebook/jest/pull/8139 for justification. Tl;dr; is that a timer count should include all scheduled timers _and friends_, not just timers.

**What actually happens**

`clock.nextTick` does not contribute to the count

**How to reproduce**

```js
const lolex = require("lolex");

const clock = lolex.createClock();

clock.setTimeout(() => {}, 5);
clock.nextTick(() => {});

console.log(clock.countTimers()); // 1
```

Pull request:
fix: count microtasks in countTimers
#### Purpose (TL;DR) - mandatory
<!--
> give a concise (one or two short sentences) description of what problem is being solved by this PR
>
> Example: Fix issue #123456 by re-structuring the colour selection conditional in method `paintBlue`
-->

Fixes #238


<!--
#### Background (Problem in detail)  - optional
-->
<!--
> When relevant, give a more thorough description of what the problem the PR is trying to solve. Examples of good topics for this section are:
> * Link to an existing GitHub issue describing the problem
> * Describing the problem in greater detail than the TL;DR section above
> * How you discovered the issue if it's not already described as an issue on GitHub
> * Discussion of different approaches to solving this problem and why you chose your proposed solution
-->


<!--
#### Solution  - optional
-->
<!--
> When contributing code (and not just fixing typos, documentation and configuration), please describe why/how your solution works. This helps reviewers spot any mistakes in the implementation.
>
> Example:
> "This solution works by adding a `paintBlue()` method"
> Then your reviewer might spot a mistake in the implementation, if `paintBlue()` uses the colour red.
-->


Patch:
diff --git a/src/lolex-src.js b/src/lolex-src.js
index 27d0b6e..20e0c37 100644
--- a/src/lolex-src.js
+++ b/src/lolex-src.js
@@ -645,7 +645,7 @@ function withGlobal(_global) {
         };
 
         clock.countTimers = function countTimers() {
-            return Object.keys(clock.timers || {}).length;
+            return Object.keys(clock.timers || {}).length + (clock.jobs || []).length;
         };
 
         clock.requestAnimationFrame = function requestAnimationFrame(func) {


Test patch:
diff --git a/test/lolex-test.js b/test/lolex-test.js
index 15dc89e..3da7414 100644
--- a/test/lolex-test.js
+++ b/test/lolex-test.js
@@ -538,6 +538,11 @@ describe("lolex", function () {
             assert.equals(this.clock.countTimers(), 2);
         });
 
+        it("counts microtasks", function () {
+            this.clock.nextTick(NOOP);
+            assert.equals(this.clock.countTimers(), 1);
+        });
+
     });
 
     describe("tick", function () {