https://github.com/capaj/proxy-date
a minimal clean utility for mocking Date in any environment featuring ES6 Proxy
https://github.com/capaj/proxy-date
Last synced: about 1 year ago
JSON representation
a minimal clean utility for mocking Date in any environment featuring ES6 Proxy
- Host: GitHub
- URL: https://github.com/capaj/proxy-date
- Owner: capaj
- License: mit
- Created: 2019-01-18T12:23:43.000Z (over 7 years ago)
- Default Branch: master
- Last Pushed: 2023-01-04T21:38:37.000Z (over 3 years ago)
- Last Synced: 2024-05-06T09:42:47.638Z (about 2 years ago)
- Language: JavaScript
- Homepage:
- Size: 859 KB
- Stars: 11
- Watchers: 3
- Forks: 0
- Open Issues: 14
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
[](https://badge.fury.io/js/proxy-date)
[](https://david-dm.org/capaj/proxy-date)
# proxy-date
a minimal utility for painless mocking Date in node.js and Proxy enabled browsers

## Why yet another Date mocking library?
Yes there is plenty of them already out there(timekeeper, mockdate atc.), but this one is the only one using proxies. What's so special about proxies? It only intercepts the constructor and `.now()`. There is no monkeypatching globals like with all the other libraries. Which means any other code relying on `Date.constructor` for example won't get affected by this.
The only downside is this only works for node.js 6 and up.
## Install
```
pnpm i proxy-date -D
```
## Usage
```js
import { mockDate, unmockDate } from 'proxy-date'
mockDate('2020-01-19T00:20:20.654Z')
new Date().toISOString() //2019-01-19T00:20:20.654Z
unmockDate()
new Date().toISOString() // now
```
I like to use this with jest's snapshot tests to ensure I don't have to manually write matchers for every date that happens to occur in a snapshot.