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

add support for queueMicrotask #217

Merged
merged 1 commit into from
Oct 17, 2018
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
11 changes: 9 additions & 2 deletions src/lolex-src.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ function withGlobal(_global) {
var nextTickPresent = (_global.process && typeof _global.process.nextTick === "function");
var performancePresent = (_global.performance && typeof _global.performance.now === "function");
var hasPerformancePrototype = (_global.Performance && (typeof _global.Performance).match(/^(function|object)$/));
var queueMicrotaskPresent = (typeof _global.queueMicrotask === "function");
var requestAnimationFramePresent = (
_global.requestAnimationFrame && typeof _global.requestAnimationFrame === "function"
);
Expand Down Expand Up @@ -487,6 +488,9 @@ function withGlobal(_global) {
if (requestAnimationFramePresent) {
timers.requestAnimationFrame = _global.requestAnimationFrame;
}
if (queueMicrotaskPresent) {
timers.queueMicrotask = _global.queueMicrotask;
}

if (cancelAnimationFramePresent) {
timers.cancelAnimationFrame = _global.cancelAnimationFrame;
Expand Down Expand Up @@ -574,6 +578,9 @@ function withGlobal(_global) {
args: Array.prototype.slice.call(arguments, 1)
});
};
clock.queueMicrotask = function queueMicrotask(func) {
return clock.nextTick(func); // explicitly drop additional arguments
};
clock.setInterval = function setInterval(func, timeout) {
timeout = parseInt(timeout, 10);
return addTimer(clock, {
Expand Down Expand Up @@ -650,11 +657,11 @@ function withGlobal(_global) {

clock.duringTick = true;

// perform process.nextTick()s
// perform microtasks
oldNow = clock.now;
runJobs(clock);
if (oldNow !== clock.now) {
// compensate for any setSystemTime() call during process.nextTick() callback
// compensate for any setSystemTime() call during microtask callback
tickFrom += clock.now - oldNow;
tickTo += clock.now - oldNow;
}
Expand Down
41 changes: 40 additions & 1 deletion test/lolex-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ var GlobalDate = Date;

var NOOP = function NOOP() { return undefined; };
var nextTickPresent = (global.process && typeof global.process.nextTick === "function");
var queueMicrotaskPresent = (typeof global.queueMicrotask === "function");
var hrtimePresent = (global.process && typeof global.process.hrtime === "function");
var performanceNowPresent = (global.performance && typeof global.performance.now === "function");
var performanceMarkPresent = (global.performance && typeof global.performance.mark === "function");
Expand Down Expand Up @@ -2518,8 +2519,46 @@ describe("lolex", function () {
});
});

describe("microtask semantics", function () {
describe("queueMicrotask semantics", function () {
// adapted from Node's tests
var clock, called;
before( function () {
if (!queueMicrotaskPresent) { this.skip(); }
});
beforeEach( function () {
clock = lolex.createClock();
called = false;
});
it("runs without timers", function () {
clock.queueMicrotask(function () {
called = true;
});
clock.runAll();
assert(called);
});
it("runs when runMicrotasks is called on the clock", function () {
clock.queueMicrotask(function () {
called = true;
});
clock.runMicrotasks();
assert(called);
});
it("runs with timers and before them", function () {
var last = "";
clock.runMicrotasks(function () {
called = true;
last = "tick";
});
clock.setTimeout(function () {
last = "timeout";
});
clock.runAll();
assert(called);
assert.equals(last, "timeout");
});
});

describe("nextTick semantics", function () {
before( function () {
if (!nextTickPresent) { this.skip(); }
});
Expand Down