Hello,
First, I know this is technically not a bug in JSDOM or in Sinon. But the combination of those two libraries produces an infinite loop when following a pattern that is probably very common.
This is the reproduction code:
const sinon = require('sinon');
sinon.useFakeTimers();
require('jsdom'); //This line creates an infinite loop
This is caused by the new Performance API implementation (tested with JSDOM 11.6.2). The "problematic" code is:
// This function assumes the clock is accurate.
function calculateClockOffset() {
const start = Date.now();
let cur = start;
while (cur === start) {
cur = Date.now();
}
Because Sinon's fake timers mock `Date` by default, `Date.now()` will always return the same number, creating a loop.
The workaround is to use:
sandbox.useFakeTimers('setTimeout', 'clearTimeout', 'setInterval', 'clearInterval');
I suspect that using Sinon's fake timers and JSDOM is a common pattern, and developers will run into this problem in the future. I'm creating this post to start the discussion about a potential improvement (maybe stop the loop and thrown an error after 10k iterations?) and to have a link we can send to other developers complaining about this problem.
Regards,
Sergio