1. jest-date-mock
Mock `window.Date` when run unit test cases with jest. Make tests of `Date` easier.
jest-date-mock
Package: jest-date-mock
Created by: hustcc
Last modified: Sun, 21 Apr 2024 01:52:41 GMT
Version: 1.0.10
License: MIT
Downloads: 1,062,068
Repository: https://github.com/hustcc/jest-date-mock

Install

npm install jest-date-mock
yarn add jest-date-mock

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.

RELATED POST

Enhancing Vue.js Development: Harnessing the Potential of Vue-Loader

Enhancing Vue.js Development: Harnessing the Potential of Vue-Loader

Simplify Data Validation in Vue.js: A Step-by-Step Guide to Using Regex

Simplify Data Validation in Vue.js: A Step-by-Step Guide to Using Regex

Troubleshooting Made Easy: Common Issues and Solutions with vue-loader Without vue-cli

Troubleshooting Made Easy: Common Issues and Solutions with vue-loader Without vue-cli

Optimizing Webpack 4 with Vue CLI 3: Disabling the Cache-Loader

Optimizing Webpack 4 with Vue CLI 3: Disabling the Cache-Loader

Step-by-Step Guide: How to Add a Function to Your Vuex Plugin

Step-by-Step Guide: How to Add a Function to Your Vuex Plugin