Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/hustcc/jest-date-mock
🌗 Mock `Date` when run unit test cases with jest. Make tests of Date easier.
https://github.com/hustcc/jest-date-mock
date datetime jest jest-date-mock jest-mock timestamp
Last synced: 7 days ago
JSON representation
🌗 Mock `Date` when run unit test cases with jest. Make tests of Date easier.
- Host: GitHub
- URL: https://github.com/hustcc/jest-date-mock
- Owner: hustcc
- License: mit
- Created: 2018-05-27T03:30:29.000Z (over 6 years ago)
- Default Branch: master
- Last Pushed: 2024-04-21T01:52:01.000Z (8 months ago)
- Last Synced: 2024-05-20T13:32:59.481Z (7 months ago)
- Topics: date, datetime, jest, jest-date-mock, jest-mock, timestamp
- Language: JavaScript
- Homepage: https://github.com/hustcc/jest-date-mock
- Size: 34.2 KB
- Stars: 261
- Watchers: 4
- Forks: 15
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Funding: .github/FUNDING.yml
- License: LICENSE
Awesome Lists containing this project
- awesome-jest - jest-date-mock
README
# jest-date-mock
> Mock `Date` when run unit test cases with jest. Make tests of `Date` easier.
> - [jest-random-mock](https://github.com/hustcc/jest-random-mock) Mock `Math.random` in jest, with deterministic random generator.
> - [jest-canvas-mock](https://github.com/hustcc/jest-canvas-mock) Mock `canvas` when run unit test cases with jest.[![Build Status](https://github.com/hustcc/jest-date-mock/workflows/build/badge.svg)](https://github.com/hustcc/jest-date-mock/actions)
[![Coverage Status](https://coveralls.io/repos/github/hustcc/jest-date-mock/badge.svg?branch=master)](https://coveralls.io/github/hustcc/jest-date-mock)
[![npm](https://img.shields.io/npm/v/jest-date-mock.svg)](https://www.npmjs.com/package/jest-date-mock)
[![npm](https://img.shields.io/npm/dm/jest-date-mock.svg)](https://www.npmjs.com/package/jest-date-mock)## Install
This should only be installed as a development dependency (`devDependencies`) as it is only designed for testing.
```bash
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.
```js
{
"jest": {
"setupFiles": ["jest-date-mock"]
}
}
```If you already have a `setupFiles` attribute you can also append `jest-date-mock` to the array.
```js
{
"jest": {
"setupFiles": ["./__setups__/other.js", "jest-date-mock"]
}
}
```More about in [configuration section](https://facebook.github.io/jest/docs/en/configuration.html#content).
## 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`
```js
import 'jest-date-mock';
// or
require('jest-date-mock');
```Add that file to your `setupFiles` array:
```js
"jest": {
"setupFiles": [
"./__setups__/date.js"
]
}
```## Usage
> Use the only `3 api` for test cases.
- `advanceBy(ms)`: advance date timestamp by `ms`.
- `advanceTo([timestamp])`: reset date to `timestamp`, default to `0`.
- `clear()`: shut down the mock system.```js
import { advanceBy, advanceTo, clear } from 'jest-date-mock';test('usage', () => {
advanceTo(new Date(2018, 5, 27, 0, 0, 0)); // reset to date time.const now = Date.now();
advanceBy(3000); // advance time 3 seconds
expect(+new Date() - now).toBe(3000);advanceBy(-1000); // advance time -1 second
expect(+new Date() - now).toBe(2000);clear();
Date.now(); // will got current timestamp
});
```More sample code [here](__tests__).
Also, add an API `Date.current()` to get the actual current timestamp.
```js
import { advanceBy, advanceTo, clear } from 'jest-date-mock';advanceTo(0); // reset to timestamp = 0
Date.now(); // will got 0
Date.current(); // will got the actual timestamp.
```## License
MIT@[hustcc](https://github.com/hustcc).