proxy-date
TypeScript icon, indicating that this package has built-in type declarations

0.9.1 • Public • Published

npm version deps

proxy-date

a minimal utility for painless mocking Date in node.js and Proxy enabled browsers

gif

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

npm i proxy-date -D
yarn add proxy-date -D

Usage

import { mock, unmock } from 'proxy-date'

mock('2020-01-19T00:20:20.654Z')
new Date().toISOString() //2019-01-19T00:20:20.654Z

unmock()
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.

Package Sidebar

Install

npm i proxy-date

Weekly Downloads

107

Version

0.9.1

License

MIT

Unpacked Size

11 kB

Total Files

13

Last publish

Collaborators

  • capaj