@sinonjs/fake-timers
Advanced tools
Comparing version 8.0.1 to 8.1.0
{ | ||
"name": "@sinonjs/fake-timers", | ||
"description": "Fake JavaScript timers", | ||
"version": "8.0.1", | ||
"version": "8.1.0", | ||
"homepage": "https://github.com/sinonjs/fake-timers", | ||
@@ -6,0 +6,0 @@ "author": "Christian Johansen", |
@@ -153,9 +153,10 @@ # `@sinonjs/fake-timers` | ||
| Parameter | Type | Default | Description | | ||
| -------------------------- | ----------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | | ||
| `config.now` | Number/Date | 0 | installs FakeTimers with the specified unix epoch | | ||
| `config.toFake` | String[] | ["setTimeout", "clearTimeout", "setImmediate", "clearImmediate","setInterval", "clearInterval", "Date", "requestAnimationFrame", "cancelAnimationFrame", "requestIdleCallback", "cancelIdleCallback", "hrtime"] | an array with explicit function names to hijack. _When not set, FakeTimers will automatically fake all methods **except** `nextTick`_ e.g., `FakeTimers.install({ toFake: ["setTimeout","nextTick"]})` will fake only `setTimeout` and `nextTick` | | ||
| `config.loopLimit` | Number | 1000 | the maximum number of timers that will be run when calling runAll() | | ||
| `config.shouldAdvanceTime` | Boolean | false | tells FakeTimers to increment mocked time automatically based on the real system time shift (e.g. the mocked time will be incremented by 20ms for every 20ms change in the real system time) | | ||
| `config.advanceTimeDelta` | Number | 20 | relevant only when using with `shouldAdvanceTime: true`. increment mocked time by `advanceTimeDelta` ms every `advanceTimeDelta` ms change in the real system time. | | ||
| Parameter | Type | Default | Description | | ||
| -------------------------------- | ----------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | | ||
| `config.now` | Number/Date | 0 | installs FakeTimers with the specified unix epoch | | ||
| `config.toFake` | String[] | ["setTimeout", "clearTimeout", "setImmediate", "clearImmediate","setInterval", "clearInterval", "Date", "requestAnimationFrame", "cancelAnimationFrame", "requestIdleCallback", "cancelIdleCallback", "hrtime"] | an array with explicit function names to hijack. _When not set, FakeTimers will automatically fake all methods **except** `nextTick`_ e.g., `FakeTimers.install({ toFake: ["setTimeout","nextTick"]})` will fake only `setTimeout` and `nextTick` | | ||
| `config.loopLimit` | Number | 1000 | the maximum number of timers that will be run when calling runAll() | | ||
| `config.shouldAdvanceTime` | Boolean | false | tells FakeTimers to increment mocked time automatically based on the real system time shift (e.g. the mocked time will be incremented by 20ms for every 20ms change in the real system time) | | ||
| `config.advanceTimeDelta` | Number | 20 | relevant only when using with `shouldAdvanceTime: true`. increment mocked time by `advanceTimeDelta` ms every `advanceTimeDelta` ms change in the real system time. | | ||
| `config.shouldClearNativeTimers` | Boolean | false | tells FakeTimers to clear 'native' (i.e. not fake) timers by delegating to their respective handlers. These are not cleared by default, leading to potentially unexpected behavior if timers existed prior to installing FakeTimers. | | ||
@@ -162,0 +163,0 @@ ### `var id = clock.setTimeout(callback, timeout)` |
@@ -86,2 +86,3 @@ "use strict"; | ||
* @property {Function[]} methods - the methods that are faked | ||
* @property {boolean} [shouldClearNativeTimers] inherited from config | ||
*/ | ||
@@ -99,2 +100,3 @@ /* eslint-enable jsdoc/require-property-description */ | ||
* @property {number} [advanceTimeDelta] increment mocked time every <<advanceTimeDelta>> ms (default: 20ms) | ||
* @property {boolean} [shouldClearNativeTimers] forwards clear timer calls to native functions if they are not fakes (default: false) | ||
*/ | ||
@@ -139,2 +141,3 @@ | ||
const maxTimeout = Math.pow(2, 31) - 1; //see https://heycam.github.io/webidl/#abstract-opdef-converttoint | ||
const idCounterStart = 1e12; // arbitrarily large number to avoid collisions with native timer IDs | ||
const NOOP = function () { | ||
@@ -198,3 +201,3 @@ return undefined; | ||
const NativeDate = _global.Date; | ||
let uniqueTimerId = 1; | ||
let uniqueTimerId = idCounterStart; | ||
@@ -567,2 +570,7 @@ /** | ||
if (timer.hasOwnProperty("idleCallback")) { | ||
timer.type = "IdleCallback"; | ||
timer.idleCallback = true; | ||
} | ||
if (!clock.timers) { | ||
@@ -581,3 +589,2 @@ clock.timers = {}; | ||
const res = { | ||
id: timer.id, | ||
ref: function () { | ||
@@ -594,2 +601,5 @@ return res; | ||
}, | ||
[Symbol.toPrimitive]: function () { | ||
return timer.id; | ||
}, | ||
}; | ||
@@ -737,2 +747,36 @@ return res; | ||
/** | ||
* Gets clear handler name for a given timer type | ||
* @param {string} ttype | ||
*/ | ||
function getClearHandler(ttype) { | ||
if (ttype === "IdleCallback" || ttype === "AnimationFrame") { | ||
return `cancel${ttype}`; | ||
} | ||
return `clear${ttype}`; | ||
} | ||
/** | ||
* Gets schedule handler name for a given timer type | ||
* @param {string} ttype | ||
*/ | ||
function getScheduleHandler(ttype) { | ||
if (ttype === "IdleCallback" || ttype === "AnimationFrame") { | ||
return `request${ttype}`; | ||
} | ||
return `set${ttype}`; | ||
} | ||
/** | ||
* Creates an anonymous function to warn only once | ||
*/ | ||
function createWarnOnce() { | ||
let calls = 0; | ||
return function (msg) { | ||
// eslint-disable-next-line | ||
!calls++ && console.warn(msg); | ||
}; | ||
} | ||
const warnOnce = createWarnOnce(); | ||
/** | ||
* @param {Clock} clock | ||
@@ -753,6 +797,21 @@ * @param {number} timerId | ||
// in Node, timerId is an object with .ref()/.unref(), and | ||
// its .id field is the actual timer id. | ||
const id = typeof timerId === "object" ? timerId.id : timerId; | ||
// in Node, the ID is stored as the primitive value for `Timeout` objects | ||
// for `Immediate` objects, no ID exists, so it gets coerced to NaN | ||
const id = Number(timerId); | ||
if (Number.isNaN(id) || id < idCounterStart) { | ||
const handlerName = getClearHandler(ttype); | ||
if (clock.shouldClearNativeTimers === true) { | ||
const nativeHandler = clock[`_${handlerName}`]; | ||
return typeof nativeHandler === "function" | ||
? nativeHandler(timerId) | ||
: undefined; | ||
} | ||
warnOnce( | ||
`FakeTimers: ${handlerName} was invoked to clear a native timer instead of one created by this library.` + | ||
"\nTo automatically clean-up native timers, use `shouldClearNativeTimers`." | ||
); | ||
} | ||
if (clock.timers.hasOwnProperty(id)) { | ||
@@ -768,10 +827,4 @@ // check that the ID matches a timer of the correct type | ||
} else { | ||
const clear = | ||
ttype === "AnimationFrame" | ||
? "cancelAnimationFrame" | ||
: `clear${ttype}`; | ||
const schedule = | ||
timer.type === "AnimationFrame" | ||
? "requestAnimationFrame" | ||
: `set${timer.type}`; | ||
const clear = getClearHandler(ttype); | ||
const schedule = getScheduleHandler(timer.type); | ||
throw new Error( | ||
@@ -1063,9 +1116,10 @@ `Cannot clear timer: timer created with ${schedule}() but cleared with ${clear}()` | ||
: Math.min(timeout, timeToNextIdlePeriod), | ||
idleCallback: true, | ||
}); | ||
return result.id || result; | ||
return Number(result); | ||
}; | ||
clock.cancelIdleCallback = function cancelIdleCallback(timerId) { | ||
return clearTimer(clock, timerId, "Timeout"); | ||
return clearTimer(clock, timerId, "IdleCallback"); | ||
}; | ||
@@ -1172,3 +1226,3 @@ | ||
return result.id || result; | ||
return Number(result); | ||
}; | ||
@@ -1597,2 +1651,4 @@ | ||
config.advanceTimeDelta = config.advanceTimeDelta || 20; | ||
config.shouldClearNativeTimers = | ||
config.shouldClearNativeTimers || false; | ||
@@ -1607,2 +1663,3 @@ if (config.target) { | ||
const clock = createClock(config.now, config.loopLimit); | ||
clock.shouldClearNativeTimers = config.shouldClearNativeTimers; | ||
@@ -1609,0 +1666,0 @@ clock.uninstall = function () { |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
89082
5
1512
354