Skip to content

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

License

Notifications You must be signed in to change notification settings

JoshuaKGoldberg/sinon-timers-repeatable

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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.

About

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

Resources

License

Code of conduct

Security policy

Stars

Watchers

Forks

Releases

No releases published

Sponsor this project

 

Packages

No packages published