Socket
Socket
Sign inDemoInstall

mockdate

Package Overview
Dependencies
0
Maintainers
1
Versions
19
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    mockdate

A JavaScript mock Date object that can be used to change when "now" is.


Version published
Weekly downloads
1.5M
increased by0.72%
Maintainers
1
Install size
14.2 kB
Created
Weekly downloads
 

Package description

What is mockdate?

The mockdate npm package is designed for testing JavaScript code that relies on dates. It allows developers to temporarily 'mock' or 'fake' the current date and time, making it possible to simulate specific temporal conditions in tests without altering the system's actual clock. This is particularly useful for ensuring consistent test results and for testing time-sensitive code paths.

What are mockdate's main functionalities?

Set a mock date

This feature allows you to set the current date and time to a specific value. It's useful for testing how your application behaves on specific dates and times.

MockDate.set('2023-04-01T00:00:00.000Z');

Reset to the system's current date

After setting a mock date, you can use this feature to reset the system's date and time back to its real current value. This is particularly useful to clean up after tests that require mocking the date.

MockDate.reset();

Set a mock date with a specific timezone offset

In addition to setting a mock date, you can specify a timezone offset in minutes. This allows for more precise control over the mocked date and time, accommodating tests that are sensitive to timezones.

MockDate.set('2023-04-01T00:00:00.000Z', 120);

Other packages similar to mockdate

Readme

Source

MockDate

A JavaScript Mock Date object that can be used to change when "now" is.

Build Status

Installation

npm install mockdate --save-dev

Environment Support

MockDate has been tested in Node, IE9+, Chrome, Firefox, and Opera.

Usage

import MockDate from 'mockdate'

API

MockDate.set(date)
date

date: Object

The Date to be returned when no parameters are passed to new Date(). Supports any object that has a .valueOf method that returns a value that can be passed to new Date().

date: String

The string representation of the date which is passed to the new Date() constructor. This creates the Date to be returned when no parameters are passed to new Date().

date: Number

The millisecond representation of the Date to be returned when no parameters are passed to new Date().

MockDate.reset();

Will restore the original Date object back to the native implementation.

Example

MockDate.set('2000-11-22');

new Date().toString() // "Tue Nov 21 2000 18:00:00 GMT-0600 (CST)"

MockDate.set('1/30/2000');

new Date().toString() // "Sun Jan 30 2000 00:00:00 GMT-0600 (CST)"

MockDate.set(new Date('2/20/2000'));

new Date().toString() // "Sun Feb 20 2000 00:00:00 GMT-0600 (CST)"

MockDate.set(moment('3/30/2000').toDate()); // using momentjs

new Date().toString() // "Thu Mar 30 2000 00:00:00 GMT-0600 (CST)"

MockDate.reset();

new Date().toString() // "Mon Mar 17 2014 18:08:44 GMT-0500 (CDT)"

Test

npm test

Keywords

FAQs

Last updated on 25 Mar 2021

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc