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

Core: Fix compatibility with sinon.useFakeTimers in IE #1738

Merged
merged 4 commits into from
Feb 8, 2024
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
18 changes: 11 additions & 7 deletions lib/promise-polyfill.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ Patches for use in QUnit:

- 2021-01-10: Add 'globalThis' to globalNS implementation to support SpiderMonkey.

- 2024-02-07: Save unmodified setImmediate to avoid conflicts with Sinon fake timers.

*/

(function () { 'use strict';
Expand Down Expand Up @@ -373,16 +375,18 @@ Promise.race = function(arr) {
};

// Use polyfill for setImmediate for performance gains
Promise._immediateFn =
// @ts-ignore
Krinkle marked this conversation as resolved.
Show resolved Hide resolved
if (typeof setImmediate === 'function') {
// @ts-ignore
(typeof setImmediate === 'function' &&
function(fn) {
// @ts-ignore
setImmediate(fn);
}) ||
function(fn) {
var setImmediateFunc = setImmediate;
Promise._immediateFn = function(fn) {
setImmediateFunc(fn);
};
} else {
Promise._immediateFn = function(fn) {
setTimeoutFunc(fn, 0);
};
}

Promise._unhandledRejectionFn = function _unhandledRejectionFn(err) {
if (typeof console !== 'undefined' && console) {
Expand Down
46 changes: 46 additions & 0 deletions test/main/promise.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,27 @@ var defer = typeof setTimeout !== 'undefined'
Promise.resolve().then(fn);
};

// Get the global namespace the same way as the Promise polyfill.
var globalNS = (function () {
if (typeof globalThis !== 'undefined') {
// eslint-disable-next-line no-undef
return globalThis;
}
if (typeof self !== 'undefined') {
// eslint-disable-next-line no-undef
return self;
}
if (typeof window !== 'undefined') {
// eslint-disable-next-line no-undef
return window;
}
if (typeof global !== 'undefined') {
// eslint-disable-next-line no-undef
return global;
}
throw new Error('unable to locate global object');
})();

// NOTE: Adds 1 assertion
function createMockPromise (assert, reject, value) {
if (arguments.length < 3) {
Expand Down Expand Up @@ -247,4 +268,29 @@ QUnit.module('Support for Promise', function () {

return createMockPromise(assert, true, new Error('this is an error'));
});

QUnit.module('compatible with fake timers', {
beforeEach: function (assert) {
this.setTimeout = globalNS.setTimeout;
globalNS.setTimeout = function () {};
if (globalNS.setImmediate) {
this.setImmediate = globalNS.setImmediate;
globalNS.setImmediate = function () {};
}
// Adds 1 assertion
return createMockPromise(assert);
},
afterEach: function (assert) {
globalNS.setTimeout = this.setTimeout;
if (this.setImmediate) {
globalNS.setImmediate = this.setImmediate;
}
// Adds 1 assertion
return createMockPromise(assert);
}
});

QUnit.test('test', function (assert) {
assert.expect(2);
});
});