• Stars
    star
    259
  • Rank 151,883 (Top 4 %)
  • Language
    JavaScript
  • License
    MIT License
  • Created almost 6 years ago
  • Updated about 1 year ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

๐ŸŒ— Mock `Date` when run unit test cases with jest. Make tests of Date easier.

jest-date-mock

Mock Date when run unit test cases with jest. Make tests of Date easier.

Build Status Coverage Status npm npm

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

import 'jest-date-mock';
// or
require('jest-date-mock');

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 by ms.
  • advanceTo([timestamp]): reset date to timestamp, default to 0.
  • clear(): shut down the mock system.
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.

Also, add an API Date.current() to get the actual current timestamp.

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.

More Repositories

1

JS-Sorting-Algorithm

ไธ€ๆœฌๅ…ณไบŽๆŽ’ๅบ็ฎ—ๆณ•็š„ GitBook ๅœจ็บฟไนฆ็ฑ ใ€Šๅๅคง็ปๅ…ธๆŽ’ๅบ็ฎ—ๆณ•ใ€‹๏ผŒๅคš่ฏญ่จ€ๅฎž็Žฐใ€‚
Java
5,082
star
2

timeago.js

๐Ÿ•— โŒ› timeago.js is a tiny(2.0 kb) library used to format date with `*** time ago` statement.
TypeScript
5,033
star
3

echarts-for-react

โ›ณ๏ธ Apache ECharts components for React wrapper. ไธ€ไธช็ฎ€ๅ•็š„ Apache echarts ็š„ React ๅฐ่ฃ…ใ€‚
TypeScript
4,321
star
4

canvas-nest.js

โ™‹ Interactive Particle / Nest System With JavaScript and Canvas, no jQuery.
JavaScript
4,253
star
5

PyG2Plot

๐ŸŽจ Python3 binding for `@AntV/G2Plot` Plotting Library .
Python
1,012
star
6

PTHospital.chrome

โš ๏ธ ๐Ÿฅ Chromeๆต่งˆๅ™จๆ’ไปถโ€”โ€”ๆ‰“ๅผ€่Ž†็”ฐ็ณปๅŒป้™ข็ฝ‘็ซ™๏ผŒๅ‘ๅ‡บ่ญฆๅ‘Šๆ้†’ใ€ๆ˜พ็คบๅŒป้™ขไฟกๆฏใ€‚
JavaScript
573
star
7

onfire.js

๐Ÿ”ซ onfire.js is a nano version (~500b) for event-emitter.
TypeScript
498
star
8

timeago-react

๐Ÿ•— Simple and efficient react component to format date with `*** time ago` statement. eg: '3 hours ago'.
TypeScript
416
star
9

xmorse

๐ŸŒž ~1.5Kb morse code library for all. ไธ€ไธชๆ”ฏๆŒ Unicode ไธญๆ–‡ๆ‘ฉๆ–ฏๅฏ†็ ็ผ–็ ็š„ Javascript ๅบ“ใ€‚
TypeScript
312
star
10

jest-canvas-mock

๐ŸŒ— A module used to mock canvas in Jest.
JavaScript
300
star
11

gantt-for-react

๐ŸŒฟ Frappe Gantt components for React wrapper. ไธ€ไธช็ฎ€ๅ•็š„็”˜็‰นๅ›พ React ็ป„ไปถๅฐ่ฃ…ใ€‚
JavaScript
291
star
12

react-adsense

๐Ÿ“ฝ๏ธ a simple React-component for Google AdSense / Baidu advertisement.
JavaScript
263
star
13

timeago

โŒ› Simple library used to format datetime with `*** time ago` statement. eg: "3 hours ago".
Python
221
star
14

ribbon.js

๐ŸŽ€ Only 1kb javascript gist to generate a ribbon in your website with HTML5 canvas.
JavaScript
219
star
15

webhookit

:octocat: Simple git webhook cli tool for automation tasks, bind git webhook to action.
Python
192
star
16

alimask

๐Ÿ˜ท alimask ๆ˜ฏไธ€ไธชไฝฟ็”จ canvas ็”Ÿๆˆ็ฑปไผผ้˜ฟ้‡Œๅทดๅทดๅ†…้ƒจ็ฝ‘็ซ™ๆฐดๅฐๅ›พ็‰‡็š„ JavaScript ๅบ“ใ€‚
JavaScript
176
star
17

size-sensor

๐ŸŒฟ 1Kb DOM element size sensor which will callback when size changed.
JavaScript
166
star
18

placeholder.js

โšก < 1Kb library to generate image placeholders on client side
PHP
161
star
19

jest-electron

โฏ โš›๏ธThe easiest way to run and debug test cases in electron with jest.
TypeScript
144
star
20

chrome-qrcode

โšก A Chrome plugin to Genrate QRCode of URL / Text, or Decode the QRcode in website. ไธ€ไธชChromeๆต่งˆๅ™จๆ’ไปถ๏ผŒ็”จไบŽ็”Ÿๆˆๅฝ“ๅ‰URLๆˆ–่€…้€‰ไธญๅ†…ๅฎน็š„ไบŒ็ปด็ ๏ผŒๅŒๆ—ถๅฏไปฅ็”จไบŽ่งฃๆž็ฝ‘้กตไธŠ็š„ไบŒ็ปด็ ๅ†…ๅฎนใ€‚
JavaScript
133
star
21

iReact

๐ŸŽ iReact: Fantastic React Components and Libraries! Makes it easy for you to pick one thatโ€™ll work for you.
JavaScript
82
star
22

mac.py

โšกA python lib to search Manufacturer of physical network card by mac address. ไธ€ไธช้€š่ฟ‡็‰ฉ็†ๅœฐๅ€ๆŸฅ่ฏข็ฝ‘ๅกๆ‰€ๅฑžๅŽ‚ๅ•†็š„Pythonๅบ“ใ€‚
Python
68
star
23

pixi-action

๐Ÿ‡ pixi-action is a plugin for Pixi.js to create actions and animations easily, inspired by Cocos2d-x.
HTML
65
star
24

word-table

โ—ผ๏ธ a simple javascript (browserjs / nodejs) library for drawing tables in the terminal / console, unicode supported.
JavaScript
60
star
25

wrapcache

โšก A python Function / Method OUTPUT cache system base on function Decorators.
Python
58
star
26

simple-shields

๐Ÿ”ฐ a simple Shields Badge service with PHP & SVG, Simple, Fast, Stable and Easy Deploy.
HTML
50
star
27

GitHub-Helper.Chrome

:octocat: A Chrome plugin for GitHub Helper. Get a better idea about GitHub.
CSS
40
star
28

GitHub-Repo-Widget.js

:octocat: A good looking github repository widget to add to your website. Not depends on jQuery or Other.
JavaScript
39
star
29

aliyun-oss-deploy

๐Ÿ™ˆ ไธ€ไธช nodejs ๅ‘ฝไปค่กŒๅทฅๅ…ท๏ผŒ็”จไบŽ้ƒจ็ฝฒ้™ๆ€่ต„ๆบๅˆฐ aliyun oss๏ผŒๆ”ฏๆŒไปฃ็ ๆ–นๅผๅ’Œ CLI ๆ–นๅผ๏ผ
JavaScript
35
star
30

slice.js

๐ŸŒŒ nano library to enhance String.substring / Array.slice with python slice style by Proxy.
JavaScript
34
star
31

hint

้‡ๆž„ๅˆฐ ---> https://github.com/hustcc/lint-md
Python
34
star
32

G2Plot-QRCode

Draw a QRCode with G2Plot.
TypeScript
33
star
33

tplv

๐Ÿ‘ฃ Nano string template library for modern, based on ES6 template string syntax.
TypeScript
33
star
34

post-messenger

๐Ÿ‘ถ ~1 Kb wrapper of window.postMessage for cross-document communication.
JavaScript
28
star
35

filesize.js

๐Ÿ”ฅ filesize.js is a simple(~360 b) module to make file size human-readable.
TypeScript
26
star
36

variable-type

๐Ÿ‘ ~ 1 kb. Schema validation. ไธ€ไธชๅชๆœ‰ 1 kb ็š„็”จไบŽๅ˜้‡็ป“ๆž„ๆ ก้ชŒ็š„ๅบ“ใ€‚
TypeScript
24
star
37

hrn

๐Ÿ”ข Type module to format number into Human-Readable-String. e.g. 4567 -> 4.6 k.
JavaScript
23
star
38

miz

๐ŸŽฏ Generate fake data, Just like a person.
TypeScript
22
star
39

React-G2Plot

๐Ÿค– Unofficial react component wrapper for @antvis/G2Plot
TypeScript
21
star
40

return-top

Pure javascript achieve click image to return top of website with animation
JavaScript
19
star
41

xhr.js

๐ŸŒŽ xhr.js is a library(< 2Kb) to make AJAX/HTTP requests with XMLHttpRequest.
JavaScript
19
star
42

jchatting

Chat system developed by Java Swing & Java Socket. ไฝฟ็”จJava Swingๅ’ŒJava Socket APIๅฎž็Žฐ็š„่Šๅคฉ็ณป็ปŸใ€‚
Java
18
star
43

warpart

ไฝฟ็”จ Flask ๅš็š„ไธ€ไธชๅ”่ฏ—ๅฑ•็คบ็ฝ‘็ซ™ใ€‚
Python
18
star
44

G2-50-Questions

โ“ 500 Questions about G2.
15
star
45

diff-text

Just get the diff of a simple inline text, simple mode.
JavaScript
14
star
46

PrimeTable

Primes Table which Less then Ten million (10000000) ไธ€ๅƒไธ‡ไปฅๅ†…็š„็ด ๆ•ฐ่กจ๏ผˆ่ดจๆ•ฐ่กจ๏ผ‰ใ€‚
JavaScript
13
star
47

ding-robot

๐Ÿค– ้’‰้’‰ๆœบๅ™จไบบ SDK for Node.jsใ€‚
JavaScript
13
star
48

babel-plugin-optimize-i18n

๐Ÿ”‘ ไผ˜ๅŒ–ๅ›ฝ้™…ๅŒ–ๆ–‡ๆกˆ็š„ Babel ๆ’ไปถ๏ผŒๅฏไปฅๅ‡ๅฐ‘ๅ›ฝ้™…ๅŒ–ๆ–‡ๆกˆ 40% ~ 50% ็š„ bundle sizeใ€‚
JavaScript
13
star
49

bmt

๐Ÿ“ in Browser, Measure Text width.
TypeScript
12
star
50

limit-size

๐ŸŒค๏ธ Lightweight, Convenient, Fast command tool to control your file size by CI, size-limit is too bloated.
TypeScript
11
star
51

yuque-lint

Markdown style lint service for Yuque.com base on webhook.
JavaScript
11
star
52

uri-parse

๐Ÿ”— Mini data-uri parser for nodejs and browser. No dependencies!
JavaScript
11
star
53

KeyHelper

๐Ÿ˜ญ ๅฅฝๆ€€ๅฟต^_^ ๅŸบไบŽ Java Swing ็š„ไธ€ๆฌพ้”ฎ็›˜ๆ”น้”ฎ็จ‹ๅบ๏ผŒๅˆ่กทๆ˜ฏ็”จไบŽ Dota ็‰ฉๅ“ๅ’Œ่‹ฑ้›„ๆŠ€่ƒฝๆ”น้”ฎใ€‚
Java
11
star
54

horse.js

๐Ÿด < 3Kb. autocomplete component of pure javascript. ็บฏJavascriptๅฎž็Žฐ็š„่ฝป้‡็ฎ€ๆด็š„ๆ็คบ็ป„ไปถใ€‚
JavaScript
11
star
55

g2-for-react

๐Ÿ“ˆg2 ๅœจ React ไธŠ็š„็ฎ€ๅ•็ป„ไปถๅŒ…่ฃ…ใ€‚React component wrapper of @antvis g2.
JavaScript
11
star
56

clz

โœ๏ธ Serverless & Beautiful third-part comment system base on lean cloud.
TypeScript
11
star
57

page-fps

๐Ÿ–ผ๏ธ Inject fps indicator into your page for web performance.
TypeScript
8
star
58

byte-parser

8๏ธโƒฃ (130B) Parse byte string to byte number, e.g. 1.2 Kb -> 1228.8, Kb, Mb, Gb, Tb, Pb, Eb, Zb, Yb supported.
TypeScript
7
star
59

immutability-util

๐ŸŒฟ Mutate a copy of data without changing the original source by path string, just like the get/set in lodash. Inspired by immutability-helper and rewrite with ES6.
JavaScript
7
star
60

npm-robbery

๐Ÿšฉ A command tool to register npm package name conveniently.
JavaScript
6
star
61

radix.js

๐Ÿ’ฏ a simple javascript module to convent number to your radix, decimal supported.
JavaScript
6
star
62

evenly

๐Ÿ’ฆ How to divide the gold evenly? No surplus. ๆ€Žไนˆๅ‡ๅŒ€ๅนถไธ”ๆ— ้—ๆผ็š„ๅˆ†้…้ป„้‡‘๏ผŸ
JavaScript
6
star
63

TODO

๐Ÿ“ A simple github authorized application for server multi-user TODO list.
JavaScript
6
star
64

react-testing-demo

๐Ÿ Ensure code quality of React components with jest, enzyme, jsdom, eslint.
JavaScript
5
star
65

jest-less-loader

โš›๏ธ Jest transformer for .less file.
TypeScript
5
star
66

anyplot

๐ŸŸช Web Components for Visualization based on G2.
TypeScript
5
star
67

babel-plugin-version

๐Ÿท๏ธ babel plugin replace define identifier `__VERSION__` to pkg.version!
JavaScript
5
star
68

rc-size-sensor

React component wrapper for DOM element size sensor.
TypeScript
4
star
69

charts-perf

Performance tests for charts library.
TypeScript
4
star
70

short-unique-string

๐Ÿ†Ž Generate short unique id string locally, used optimize javascript code.
JavaScript
3
star
71

hustcc.github.io

Code for my GitHub profile.
HTML
3
star
72

what.js

๐Ÿ‘ป only 0.25 kb. what.js is a simple library to get the type of variables. enganced typeof function.
JavaScript
3
star
73

chart-entanglement

ๆจกไปฟ้‡ๅญ็‰นๆ•ˆ็บ ็ผ ๅฎž็Žฐๅ›พ่กจไน‹้—ด็š„่”ๅŠจใ€‚
JavaScript
3
star
74

jest-url-loader

โš›๏ธ Similar with webpack's url-loader for Jest.
TypeScript
3
star
75

browser-support-badge

Generate browser version compatibility badges
JavaScript
3
star
76

emoticon.js

{๏ธถใ€‚๏ธถ}โ”› simple javascript library to generate random text emoticon.
JavaScript
3
star
77

hustcc

2
star
78

the-git-commit

:octocat: Nano nodejs module to get information of any git commit.
JavaScript
2
star
79

inject-version

Inject version into your source code when prepublishOnly.
TypeScript
2
star
80

string-shorten

Shorten a string with crc32 and radix.
JavaScript
2
star
81

jest-expect

๐Ÿƒ๐Ÿ˜‚ Make jest expect more convenient.
JavaScript
2
star
82

gallery

Donate page.
CSS
1
star
83

template

Repository template for @hustcc
JavaScript
1
star
84

pypi-demo

Just a simple pypi package code structure. You can use it to register pypi package name.
Python
1
star