# sinon-timers-repeatable

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

Latest version **0.0.2** (published 2022-08-13) · MIT license · 0 weekly downloads

## Install

```sh
npm install sinon-timers-repeatable
pnpm add sinon-timers-repeatable
yarn add sinon-timers-repeatable
bun add sinon-timers-repeatable
```

## Health

**Score 45/100 (D)** — status: abandoned.

Positive: has types; esm support; no vulnerabilities; high maintenance score; high quality score.

Warnings: low downloads; pre 1.0.

Negative: abandoned.

## Facts

| | |
|---|---|
| Version | 0.0.2 |
| Published | 2022-08-13 |
| First published | 2022-08-13 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Dependencies | 2 |
| Unpacked size | 16.5 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 1 |
| Author | Josh Goldberg |
| Maintainers | joshuakgoldberg |
| Keywords | sinon, fake, clock, useFakeTimers, tests, testing, unit |

## Links

- npm: https://www.npmjs.com/package/sinon-timers-repeatable
- Repository: https://github.com/JoshuaKGoldberg/sinon-timers-repeatable
- Homepage: https://github.com/JoshuaKGoldberg/sinon-timers-repeatable/tree/main/packages/sinon-timers-repeatable
- Issues: https://github.com/JoshuaKGoldberg/sinon-timers-repeatable/issues
- npm.io page: https://npm.io/package/sinon-timers-repeatable

## Dependencies (2)

- [sinon](https://npm.io/package/sinon.md) ^14.0.0
- [@types/sinon](https://npm.io/package/@types/sinon.md) ^10.0.13

## Alternatives

- [duck](https://npm.io/package/duck.md) — 4.2M weekly downloads
- [ava](https://npm.io/package/ava.md) — 560.2K weekly downloads
- [storybook-addon-module-mock](https://npm.io/package/storybook-addon-module-mock.md) — 71.7K weekly downloads
- [@ethereum-waffle/mock-contract](https://npm.io/package/@ethereum-waffle/mock-contract.md) — 40.0K weekly downloads
- [aws-elasticsearch-connector](https://npm.io/package/aws-elasticsearch-connector.md) — 37.4K weekly downloads

## Recent versions

- 0.0.2 (latest) — 2022-08-13
- 0.0.1 — 2022-08-13

## README

# sinon-timers-repeatable

[![GitHub CI](https://github.com/JoshuaKGoldberg/sinon-timers-repeatable/actions/workflows/compile.yml/badge.svg)](https://github.com/JoshuaKGoldberg/sinon-timers-repeatable/actions/workflows/compile.yml)
[![Code Style: Prettier](https://img.shields.io/badge/code_style-prettier-brightgreen.svg)](https://prettier.io)
![TypeScript: Strict](https://img.shields.io/badge/typescript-strict-brightgreen.svg)
[![NPM version](https://badge.fury.io/js/sinon-timers-repeatable.svg)](http://badge.fury.io/js/sinon-timers-repeatable)

> A version of [Sinon](https://sinonjs.org)'s [`useFakeTimers`](https://sinonjs.org/releases/latest/fake-timers) 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:

```plaintext
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

```js
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`](./.github/CONTRIBUTING.md).
Thanks! 💖

---
_Source: https://npm.io/package/sinon-timers-repeatable · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
