Skip to content

Commit

Permalink
Fix for #483 and jestjs/jest#14549 (#485)
Browse files Browse the repository at this point in the history
* Add test from jestjs/jest#14549
* Fix for *Async time forwarders
  • Loading branch information
fatso83 authored Oct 20, 2023
1 parent f6ef392 commit 5e347dc
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 0 deletions.
3 changes: 3 additions & 0 deletions src/fake-timers-src.js
Original file line number Diff line number Diff line change
Expand Up @@ -1578,6 +1578,8 @@ function withGlobal(_global) {
function doRun() {
originalSetTimeout(function () {
try {
runJobs(clock);

let numTimers;
if (i < clock.loopLimit) {
if (!clock.timers) {
Expand Down Expand Up @@ -1633,6 +1635,7 @@ function withGlobal(_global) {
try {
const timer = lastTimer(clock);
if (!timer) {
runJobs(clock);
resolve(clock.now);
}

Expand Down
34 changes: 34 additions & 0 deletions test/issue-483-test.js
Original file line number Diff line number Diff line change
@@ -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);
});
});
});

0 comments on commit 5e347dc

Please sign in to comment.