Skip to content

Latest commit

 

History

History
93 lines (73 loc) · 3.64 KB

README.md

File metadata and controls

93 lines (73 loc) · 3.64 KB

Sinon Timers Repeatable

A version of Sinon's `useFakeTimers` that you can call multiple times in a test. ⏱️

All Contributors: 1 Codecov Test Coverage Contributor Covenant License: MIT Sponsor: On GitHub Style: Prettier TypeScript: Strict npm package version

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, calls clock.reset() after each test

That way you can call useFakeTimers() whenever you want, and not have clock state shared between tests. Hooray! ✨

Usage

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.

Development

See .github/CONTRIBUTING.md, then .github/DEVELOPMENT.md. Thanks! 💖

Contributors

Josh Goldberg
Josh Goldberg

🔧 🚧

💙 This package is based on @JoshuaKGoldberg's create-typescript-app.