Skip to content

Instantly share code, notes, and snippets.

@marcbachmann
Created March 29, 2022 20:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save marcbachmann/92d6bcaed37ba4242bdc783e8843e627 to your computer and use it in GitHub Desktop.
Save marcbachmann/92d6bcaed37ba4242bdc783e8843e627 to your computer and use it in GitHub Desktop.
A replacement for sinon.useFakeTimers
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