diff --git a/integration-test/fake-clock-integration-test.js b/integration-test/fake-clock-integration-test.js index 4964b13..f198da3 100644 --- a/integration-test/fake-clock-integration-test.js +++ b/integration-test/fake-clock-integration-test.js @@ -1,6 +1,6 @@ "use strict"; -var jsdom; +let jsdom; if (typeof require === "function" && typeof module === "object") { try { @@ -17,15 +17,15 @@ if (!jsdom) { return; } -var assert = require("@sinonjs/referee-sinon").assert; -var FakeTimers = require("../src/fake-timers-src"); -var sinon = require("@sinonjs/referee-sinon").sinon; +const assert = require("@sinonjs/referee-sinon").assert; +const FakeTimers = require("../src/fake-timers-src"); +const sinon = require("@sinonjs/referee-sinon").sinon; describe("withGlobal", function () { - var jsdomGlobal, withGlobal, timers; + let jsdomGlobal, withGlobal, timers; beforeEach(function () { - var dom = new jsdom.JSDOM("", { runScripts: "dangerously" }); + const dom = new jsdom.JSDOM("", { runScripts: "dangerously" }); jsdomGlobal = dom.window; withGlobal = FakeTimers.withGlobal(jsdomGlobal); @@ -37,8 +37,8 @@ describe("withGlobal", function () { }); it("should support basic setTimeout", function () { - var clock = withGlobal.install({ toFake: timers }); - var stub = sinon.stub(); + const clock = withGlobal.install({ toFake: timers }); + const stub = sinon.stub(); jsdomGlobal.setTimeout(stub, 5); clock.tick(5); @@ -50,7 +50,7 @@ describe("withGlobal", function () { it("Date is instanceof itself", function () { assert(new jsdomGlobal.Date() instanceof jsdomGlobal.Date); - var clock = withGlobal.install({ toFake: timers }); + const clock = withGlobal.install({ toFake: timers }); assert(new jsdomGlobal.Date() instanceof jsdomGlobal.Date); @@ -59,13 +59,15 @@ describe("withGlobal", function () { }); describe("globally configured browser objects", function () { - var withGlobal, originalDescriptors; + let withGlobal, originalDescriptors; // We use a set up function instead of beforeEach to avoid Mocha's check leaks detector function setUpGlobal() { // Configuration taken from from here https://github.com/airbnb/enzyme/blob/master/docs/guides/jsdom.md - var dom = new jsdom.JSDOM(""); - var window = dom.window; + const dom = new jsdom.JSDOM( + "", + ); + const window = dom.window; function makeMutable(descriptor) { descriptor.configurable = true; @@ -99,8 +101,8 @@ describe("globally configured browser objects", function () { } function tearDownGlobal() { - var originalDescriptorNames = Object.keys(originalDescriptors); - var windowDescriptorNames = Object.getOwnPropertyNames(global.window); + const originalDescriptorNames = Object.keys(originalDescriptors); + const windowDescriptorNames = Object.getOwnPropertyNames(global.window); windowDescriptorNames.forEach(function (descriptorName) { if (!originalDescriptorNames.includes(descriptorName)) { delete global[descriptorName]; @@ -118,8 +120,8 @@ describe("globally configured browser objects", function () { setUpGlobal(); try { - var mockNow = new Date("1990-1-1"); - var clock = withGlobal.install({ + const mockNow = new Date("1990-1-1"); + const clock = withGlobal.install({ now: mockNow, }); diff --git a/test/fake-timers-test.js b/test/fake-timers-test.js index 6ed62f1..d318c61 100644 --- a/test/fake-timers-test.js +++ b/test/fake-timers-test.js @@ -2482,6 +2482,38 @@ describe("FakeTimers", function () { assert(spies[0].calledBefore(spies[1])); }); }); + + it("should run micro-tasks scheduled between timers", async function () { + const clock = FakeTimers.createClock(); + const fake = sinon.fake(); + + clock.setTimeout(() => { + fake(2); + clock.queueMicrotask(() => fake(3)); + }, 0); + clock.setTimeout(() => { + fake(4); + clock.queueMicrotask(() => fake(5)); + }, 0); + clock.queueMicrotask(() => fake(1)); + + await clock.runAllAsync(); + assert.equals(fake.args[0][0], 1); + assert.equals(fake.args[1][0], 2); + assert.equals(fake.args[2][0], 3); + assert.equals(fake.args[3][0], 4); + assert.equals(fake.args[4][0], 5); + }); + + it("should run micro-tasks also when no timers have been scheduled", async function () { + const clock = FakeTimers.createClock(); + const fake = sinon.fake(); + + clock.queueMicrotask(() => fake(1)); + + await clock.runAllAsync(); + assert.equals(fake.args[0][0], 1); + }); }); describe("runToLast", function () { @@ -2694,7 +2726,7 @@ describe("FakeTimers", function () { }, ); - it("new timers added with a call time ealier than the last existing timer are run", function () { + it("new timers added with a call time earlier than the last existing timer are run", function () { this.clock = FakeTimers.createClock(); const test = this; const spies = [ @@ -2717,7 +2749,7 @@ describe("FakeTimers", function () { }); it( - "new timers added from a promise with a call time ealier than the last existing timer" + + "new timers added from a promise with a call time earlier than the last existing timer" + "are run", function () { this.clock = FakeTimers.createClock(); @@ -2835,6 +2867,36 @@ describe("FakeTimers", function () { assert(spies[0].calledBefore(spies[1])); }); }); + + it("should run micro-tasks scheduled between timers", async function () { + const clock = FakeTimers.createClock(); + const fake = sinon.fake(); + + clock.setTimeout(() => { + fake(1); + clock.queueMicrotask(() => fake(2)); + clock.queueMicrotask(() => fake(3)); + }, 0); + clock.setTimeout(() => { + fake(4); + }, 0); + + await clock.runToLastAsync(); + assert.equals(fake.args[0][0], 1); + assert.equals(fake.args[1][0], 2); + assert.equals(fake.args[2][0], 3); + assert.equals(fake.args[3][0], 4); + }); + + it("should run micro-tasks also when no timers have been scheduled", async function () { + const clock = FakeTimers.createClock(); + const fake = sinon.fake(); + + clock.queueMicrotask(() => fake(1)); + + await clock.runToLastAsync(); + assert.equals(fake.args[0][0], 1); + }); }); describe("clearTimeout", function () { diff --git a/test/issue-483-test.js b/test/issue-483-test.js deleted file mode 100644 index 3f6ee87..0000000 --- a/test/issue-483-test.js +++ /dev/null @@ -1,34 +0,0 @@ -"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); - }); - }); -});