Repository: sinonjs/fake-timers
Pull request #485 (2023-10-19 18:56:33)

Fix for #483 and jestjs/jest#14549
#### Purpose (TL;DR) - mandatory

Fix the async time forwarders that seemed to not run enqueued micro-tasks.

#### Background (Problem in detail)  - optional
See #483 for details

I will move the tests around, as they were used to just verify the original issue. Posting the draft here.

codecov[bot]: ## [Codecov](https://app.codecov.io/gh/sinonjs/fake-timers/pull/485?src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=sinonjs) Report
All modified and coverable lines are covered by tests :white_check_mark:
> Comparison is base [(`cfb257f`)](https://app.codecov.io/gh/sinonjs/fake-timers/commit/cfb257f6f97f4fc546c562d3cb31887fd6998bbe?el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=sinonjs) 96.48% compared to head [(`bc1a2d5`)](https://app.codecov.io/gh/sinonjs/fake-timers/pull/485?src=pr&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=sinonjs) 96.58%.
> Report is 9 commits behind head on main.

> :exclamation: Current head bc1a2d5 differs from pull request most recent head 9802829. Consider uploading reports for the commit 9802829 to get more accurate results

<details><summary>Additional details and impacted files</summary>


```diff
@@            Coverage Diff             @@
##             main     #485      +/-   ##
==========================================
+ Coverage   96.48%   96.58%   +0.10%     
==========================================
  Files           2        3       +1     
  Lines         682      702      +20     
==========================================
+ Hits          658      678      +20     
  Misses         24       24              
```

| [Flag](https://app.codecov.io/gh/sinonjs/fake-timers/pull/485/flags?src=pr&el=flags&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=sinonjs) | Coverage Δ | |
|---|---|---|
| [unit](https://app.codecov.io/gh/sinonjs/fake-timers/pull/485/flags?src=pr&el=flag&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=sinonjs) | `96.58% <100.00%> (+0.10%)` | :arrow_up: |

Flags with carried forward coverage won't be shown. [Click here](https://docs.codecov.io/docs/carryforward-flags?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=sinonjs#carryforward-flags-in-the-pull-request-comment) to find out more.

| [Files](https://app.codecov.io/gh/sinonjs/fake-timers/pull/485?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=sinonjs) | Coverage Δ | |
|---|---|---|
| [src/fake-timers-src.js](https://app.codecov.io/gh/sinonjs/fake-timers/pull/485?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=sinonjs#diff-c3JjL2Zha2UtdGltZXJzLXNyYy5qcw==) | `96.51% <100.00%> (+0.01%)` | :arrow_up: |
| [test/issue-async.test.js](https://app.codecov.io/gh/sinonjs/fake-timers/pull/485?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=sinonjs#diff-dGVzdC9pc3N1ZS1hc3luYy50ZXN0Lmpz) | `100.00% <100.00%> (ø)` | |


</details>

[:umbrella: View full report in Codecov by Sentry](https://app.codecov.io/gh/sinonjs/fake-timers/pull/485?src=pr&el=continue&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=sinonjs).   
:loudspeaker: Have feedback on the report? [Share it here](https://about.codecov.io/codecov-pr-comment-feedback/?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=sinonjs).


Patch:
diff --git a/src/fake-timers-src.js b/src/fake-timers-src.js
index d8c6a6f..0ce7217 100644
--- a/src/fake-timers-src.js
+++ b/src/fake-timers-src.js
@@ -1578,6 +1578,8 @@ function withGlobal(_global) {
                     function doRun() {
                         originalSetTimeout(function () {
                             try {
+                                runJobs(clock);
+
                                 let numTimers;
                                 if (i < clock.loopLimit) {
                                     if (!clock.timers) {
@@ -1633,6 +1635,7 @@ function withGlobal(_global) {
                         try {
                             const timer = lastTimer(clock);
                             if (!timer) {
+                                runJobs(clock);
                                 resolve(clock.now);
                             }
 


Test patch:
diff --git a/test/issue-483-test.js b/test/issue-483-test.js
new file mode 100644
index 0000000..3f6ee87
--- /dev/null
+++ b/test/issue-483-test.js
@@ -0,0 +1,34 @@
+"use strict";
+
+const FakeTimers = require("../src/fake-timers-src");
+const sinon = require("sinon");
+const assert = require("assert");
+
+function myFn(cb) {
+    queueMicrotask(() => cb());
+}
+
+describe("async time skippers should run microtasks", function () {
+    let clock;
+    const timers = ["runAllAsync", "runToLastAsync"];
+
+    afterEach(function () {
+        clock.uninstall();
+    });
+
+    beforeEach(function setup() {
+        clock = FakeTimers.install({ toFake: ["queueMicrotask"] });
+    });
+
+    // eslint-disable-next-line mocha/no-setup-in-describe
+    timers.forEach((fastForward) => {
+        it(`should advance past queued microtasks using ${fastForward}`, async function () {
+            const cb = sinon.fake();
+            myFn(cb);
+            myFn(cb);
+            myFn(cb);
+            await clock[fastForward]();
+            assert.equal(cb.callCount, 3);
+        });
+    });
+});