With the latest JSDOM (11.6.0), I get an infinite loop when used with Sinon's fake timers.
const sinon = require('sinon');
sinon.useFakeTimers();
require('jsdom'); // This line creates an infinite loop
How can I avoid this infinite loop?
This is caused by the new Performance API.
The implementation uses Date.now() to calibrate the clock. This is the function:
// This function assumes the clock is accurate.
function calculateClockOffset() {
const start = Date.now();
let cur = start;
while (cur === start) {
cur = Date.now();
}
...
}
(source, note this code is not in JSDOM but in one of its dependencies, w3c-hr-time)
When you run sinon.useFakeTimers();, it will mock Date.now() to always return the same value, therefore the above code creates an infinite loop.
The workaround is to not mock Date, only setTimeout/setInterval functions:
// Sinon 2.x
sinon.useFakeTimers('setTimeout', 'clearTimeout', 'setInterval', 'clearInterval');
// Sinon 3.x or higher
sinon.useFakeTimers({toFake:['setTimeout', 'clearTimeout', 'setInterval', 'clearInterval']});