A version of Sinon's `useFakeTimers` that you can call multiple times in a test. ⏱️
sinon.useFakeTimers()
is a wonderful API, but crashes if you call it twice in the same test:
TypeError: Can't install fake timers twice on the same global object.
at Object.install (.../node_modules/sinon/pkg/sinon.js:6783:19)
at createClock (.../node_modules/sinon/pkg/sinon.js:4676:31)
...
That can be inconvenient if you want to useFakeTimers()
inside setup functions.
This package provides a useFakeTimers
that:
- Lazily creates a
clock = sinon.useFakeTimers()
on-demand - If a global
afterAll
exists, callsclock.reset()
after each test
That way you can call useFakeTimers()
whenever you want, and not have clock state shared between tests.
Hooray! ✨
npm i sinon-timers-repeatable -D
import { useFakeTimers } from "sinon-timers-repeatable";
export function createFakes() {
return {
clock: useFakeTimers(),
// ...
};
}
useFakeTimers
takes in the same parameters as sinon.useFakeTimers
.
See .github/CONTRIBUTING.md
, then .github/DEVELOPMENT.md
.
Thanks! 💖
Josh Goldberg 🔧 🚧 |
💙 This package is based on @JoshuaKGoldberg's create-typescript-app.