# jest-mock-now

> Date.now() as deterministic Jest mock function.

Latest version **1.3.0** (published 2020-02-25) · MIT license · 0 weekly downloads

## Install

```sh
npm install jest-mock-now
pnpm add jest-mock-now
yarn add jest-mock-now
bun add jest-mock-now
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.3.0 |
| Published | 2020-02-25 |
| First published | 2017-06-23 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 0 |
| Unpacked size | 8.3 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 33 |
| Author | Mattia Richetto |
| Maintainers | mattiaerre |
| Keywords | date, jest, mock, now |

## Links

- npm: https://www.npmjs.com/package/jest-mock-now
- Repository: https://github.com/mattiaerre/jest-mock-now
- Homepage: https://github.com/mattiaerre/jest-mock-now#readme
- Issues: https://github.com/mattiaerre/jest-mock-now/issues
- npm.io page: https://npm.io/package/jest-mock-now

## Alternatives

- [@js-joda/timezone](https://npm.io/package/@js-joda/timezone.md) — 383.4K weekly downloads
- [chartjs-adapter-moment](https://npm.io/package/chartjs-adapter-moment.md) — 210.8K weekly downloads
- [strftime](https://npm.io/package/strftime.md) — 171.2K weekly downloads
- [vue-flatpickr-component](https://npm.io/package/vue-flatpickr-component.md) — 115.8K weekly downloads
- [timepicker](https://npm.io/package/timepicker.md) — 51.0K weekly downloads

## Recent versions

- 1.3.0 (latest) — 2020-02-25
- 1.2.1 — 2019-01-10
- 1.2.0 — 2018-03-06
- 1.1.1 — 2017-06-30
- 1.1.0 — 2017-06-25
- 1.0.0 — 2017-06-24
- 1.0.0-alpha.1 — 2017-06-23

## README

# jest-mock-now

`Date.now()` as deterministic [Jest mock function](https://facebook.github.io/jest/docs/mock-functions.html).

## Install

```bash
➜ ~ npm install --save-dev jest-mock-now
```

## Usage

It is possible to use the following configurations at every setup level; in a `setupJest.js` file as well as in a `beforeEach` or a `test` function as shown [here](__tests__/index.test.js).

```javascript
const timestamp = require('jest-mock-now')();

console.log(Date.now()); // 1479427200000
```

or

```javascript
const now = new Date('2017-06-22');

console.log(Date.now()); // 149808960000
```

The `jest-mock-now` function returns the timestamp used to mock the `Date.now` method:

```javascript
const timestamp = require('jest-mock-now')(new Date('2017-06-22'));

console.log(timestamp); // 1498089600000
console.log(Date.now()); // 149808960000
```

If you need to restore the original `Date.now()` method, you can call `mockRestore()`.

```javascript
Date.now.mockRestore();
```

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