sinon-timers-repeatable
v0.0.2
Published
A version of Sinon's useFakeTimers that you can call multiple times in a test.
Downloads
76
Maintainers
Readme
sinon-timers-repeatable
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! ✨
Usage
import { useFakeTimers } from "sinon-timers-repeatable";
export function createFakes() {
return {
clock: useFakeTimers(),
// ...
};
}
useFakeTimers
takes in the same parameters as sinon.useFakeTimers
.
Package Management
sinon-timers-repeatable
supports the following package formats:
- AMD (Asynchronous Module Definition):
lib/index.amd.js
- CJS (CommonJS):
lib/index.cjs
- ESM (ECMAScript Modules):
src/index.js
Development
See .github/CONTRIBUTING.md
.
Thanks! 💖