Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix for #483 and jestjs/jest#14549 #485

Merged
merged 5 commits into from
Oct 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
});
});
});