jest-date-mock
Mock
Date
when run unit test cases with jest. Make tests ofDate
easier.
Install
This should only be installed as a development dependency (devDependencies
) as it is only designed for testing.
npm i --save-dev jest-date-mock
Setup
In your package.json
under the jest
, create a setupFiles
array and add jest-date-mock
to the array.
"jest": "setupFiles": "jest-date-mock"
If you already have a setupFiles
attribute you can also append jest-date-mock
to the array.
"jest": "setupFiles": "./__setups__/other.js" "jest-date-mock"
More about in configuration section.
Setup file
Alternatively you can create a new setup file which then requires this module or
add the require
statement to an existing setup file.
__setups__/date.js
;// or;
Add that file to your setupFiles
array:
"jest": "setupFiles": "./__setups__/date.js"
Usage
Use the only
3 api
for test cases.
advanceBy(ms)
: advance date timestamp byms
.advanceTo([timestamp])
: reset date totimestamp
, default to0
.clear()
: shut down the mock system.
; ;
More sample code here.
Also, add an API Date.current()
to get the actual current timestamp.
; ; // reset to timestamp = 0 Date; // will got 0 Datecurrent; // will got the actual timestamp.
License
MIT@hustcc.