var FakeTimers = require("@sinonjs/fake-timers");
const realDate = Date;
const realSetImmediate = setImmediate;
const start = realDate.now();
function log(...o){
return console.log(realDate.now() - start + ": " + [...o].join(" "));
}
log("start")
realSetImmediate(() => log("executing using non-stubbed setImmediate, will execute after the sync script completes"));
var clock = FakeTimers.install({
shouldAdvanceTime: true,
advanceTimeDelta: 40,
toFake: ["setTimeout", "clearTimeout", "setInterval","setImmediate"]
});
const ret1 = setTimeout(() => {
log("this just timed out, because synchronous"); //executed after 40ms
}, 30);
const ret2 = setImmediate(() => {
log("not so immediate"); //executed after 40ms
});
const ret3 = setTimeout(() => {
log("this timed out after"); //executed after 80ms
clock.uninstall();
}, 50);
//log("timers: ",ret1,ret2,ret3);
// clock.tick(50); // if you want to see how it behaves with shouldAdvanceTime : false
log("sync script complete")