Created
March 29, 2022 20:05
-
-
Save marcbachmann/92d6bcaed37ba4242bdc783e8843e627 to your computer and use it in GitHub Desktop.
A replacement for sinon.useFakeTimers
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const OriginalDate = global.Date | |
function restoreFakeDate () { | |
global.Date = OriginalDate | |
} | |
function useFakeDate (date) { | |
date = new OriginalDate(date) | |
class Date extends OriginalDate { | |
constructor (...args) { | |
if (args.length) super(...args) | |
else super(date) | |
} | |
static now () { | |
return date.getTime() | |
} | |
} | |
global.Date = Date | |
return { | |
restore: restoreFakeDate, | |
tick (ms) { | |
date = new OriginalDate(date.getTime() + ms) | |
} | |
} | |
} | |
module.exports = useFakeDate |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment