Skip to content

Commit

Permalink
Refactor tests for issue #483
Browse files Browse the repository at this point in the history
Made the tests more to the point
  • Loading branch information
fatso83 committed Oct 20, 2023
1 parent 5e347dc commit cb72d77
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 52 deletions.
34 changes: 18 additions & 16 deletions 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 {
Expand All @@ -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);
Expand All @@ -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);
Expand All @@ -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);

Expand All @@ -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("<!doctype html><html><body></body></html>");
var window = dom.window;
const dom = new jsdom.JSDOM(
"<!doctype html><html><body></body></html>",
);
const window = dom.window;

function makeMutable(descriptor) {
descriptor.configurable = true;
Expand Down Expand Up @@ -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];
Expand All @@ -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,
});

Expand Down
66 changes: 64 additions & 2 deletions test/fake-timers-test.js
Expand Up @@ -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 () {
Expand Down Expand Up @@ -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 = [
Expand All @@ -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();
Expand Down Expand Up @@ -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 () {
Expand Down
34 changes: 0 additions & 34 deletions test/issue-483-test.js

This file was deleted.

0 comments on commit cb72d77

Please sign in to comment.