All Projects β†’ hustcc β†’ Jest Date Mock

hustcc / Jest Date Mock

Licence: mit
πŸŒ— Mock `Date` when run unit test cases with jest. Make tests of Date easier.

Programming Languages

javascript
184084 projects - #8 most used programming language

Projects that are alternatives of or similar to Jest Date Mock

Time Stamp
Get a formatted timestamp. Used in gulp, assemble, generate, and many others.
Stars: ✭ 104 (-53.57%)
Mutual labels:  date, datetime, timestamp
format-date
πŸ“† A small library (around 400 B when gziped & minified) to format JavaScript `Date` object using same tokens as moment.
Stars: ✭ 25 (-88.84%)
Mutual labels:  datetime, date, timestamp
Tinydate
A tiny (349B) reusable date formatter. Extremely fast!
Stars: ✭ 990 (+341.96%)
Mutual labels:  date, datetime, timestamp
Calendar
πŸ“… PHP Date & Time library that solves common problems in object oriented, immutable way.
Stars: ✭ 113 (-49.55%)
Mutual labels:  date, datetime
Go Naturaldate
Natural date/time parsing for Go.
Stars: ✭ 217 (-3.12%)
Mutual labels:  date, datetime
Dateparse
GoLang Parse many date strings without knowing format in advance.
Stars: ✭ 1,365 (+509.38%)
Mutual labels:  date, datetime
Carbon
A simple PHP API extension for DateTime
Stars: ✭ 75 (-66.52%)
Mutual labels:  date, datetime
Tail.datetime
A lightweight, translat- and configurable Open Source DateTime Picker, written in pure vanilla JavaScript!
Stars: ✭ 139 (-37.95%)
Mutual labels:  date, datetime
Horizontalpicker
DatePicker horizontal con selecciΓ³n smooth por dΓ­a para Android.
Stars: ✭ 116 (-48.21%)
Mutual labels:  date, datetime
Tj
stdin line timestamps. single binary, no dependencies. osx & linux & windows. plays well with jq.
Stars: ✭ 218 (-2.68%)
Mutual labels:  date, timestamp
Gostradamus
Gostradamus: Better DateTimes for Go πŸ•°οΈ
Stars: ✭ 148 (-33.93%)
Mutual labels:  date, datetime
Luatz
Time, Date and Timezone library for lua
Stars: ✭ 92 (-58.93%)
Mutual labels:  date, datetime
Graphql Java Datetime
GraphQL ISO Date is a set of RFC 3339 compliant date/time scalar types to be used with graphql-java.
Stars: ✭ 89 (-60.27%)
Mutual labels:  date, datetime
Date Picker
πŸ“… Custom responsive date picker widget for Android, written in Kotlin.
Stars: ✭ 146 (-34.82%)
Mutual labels:  date, datetime
Time
Building a better date/time library for Swift
Stars: ✭ 1,983 (+785.27%)
Mutual labels:  date, datetime
Rc Datetime Picker
React component for datetime picker by Moment.js
Stars: ✭ 85 (-62.05%)
Mutual labels:  date, datetime
Anytime
Anything to POSIXct or Date Converter
Stars: ✭ 137 (-38.84%)
Mutual labels:  date, datetime
Date
A date and time library based on the C++11/14/17 <chrono> header
Stars: ✭ 2,389 (+966.52%)
Mutual labels:  date, datetime
When
A natural language date/time parser with pluggable rules
Stars: ✭ 1,113 (+396.88%)
Mutual labels:  date, datetime
Iso8601
Ruby parser to work with ISO8601 dateTimes and durations β€” http://en.wikipedia.org/wiki/ISO_8601
Stars: ✭ 70 (-68.75%)
Mutual labels:  date, datetime

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

[email protected]hustcc.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].