
Product
Secure Your AI-Generated Code with Socket MCP
Socket MCP brings real-time security checks to AI-generated code, helping developers catch risky dependencies before they enter the codebase.
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.
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);
Sinon is a comprehensive testing utility that, among other features, includes the ability to stub JavaScript's Date object. Compared to mockdate, Sinon offers a broader range of testing functionalities beyond date mocking, such as spies, stubs, and mocks for JavaScript functions.
Timekeeper is a simple package for controlling the system time in JavaScript. It's similar to mockdate in its purpose of mocking the current time for testing purposes. However, timekeeper has a simpler API and fewer features, focusing solely on freezing and traveling in time without the additional timezone offset control that mockdate offers.
Lolex is a library for JavaScript and Node.js that allows for the mocking of the system clock. It provides functionalities similar to mockdate but extends its capabilities to include mocking of timers (setTimeout, setInterval, etc.). This makes lolex a more versatile choice for testing time-dependent code.
A JavaScript Mock Date object that can be used to change when "now" is.
npm install mockdate
MockDate has been tested in Node, IE9+, Chrome, Firefox, and Opera.
// CommonJS
var MockDate = require('mockdate');
// AMD
require(['MockDate'], function(MockDate) { ... });
// Script Tag
var MockDate = window.MockDate;
MockDate.set(date, [timezoneOffset])
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
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()
.
timezoneOffset: Number
The value that should be returned by new Date().getTimezoneOffset()
MockDate.reset();
Will restore the original Date
object back to the native implementation.
MockDate.set('1/1/2000');
new Date().toString() // "Sat Jan 01 2000 00:00:00 GMT-0600 (CST)"
MockDate.set('1/1/2000', 120);
new Date().getTimezoneOffset() // 120
MockDate.set(new Date('2/2/2000'));
new Date().toString() // "Wed Feb 02 2000 00:00:00 GMT-0600 (CST)"
MockDate.set(moment('3/3/2000')); // using momentjs
new Date().toString() // "Fri Mar 03 2000 00:00:00 GMT-0600 (CST)"
MockDate.reset();
new Date().toString() // "Mon Mar 17 2014 18:08:44 GMT-0500 (CDT)"
new Date().getTimezoneOffset() // 360
npm test
FAQs
A JavaScript mock Date object that can be used to change when "now" is.
The npm package mockdate receives a total of 1,413,076 weekly downloads. As such, mockdate popularity was classified as popular.
We found that mockdate demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
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.
Product
Socket MCP brings real-time security checks to AI-generated code, helping developers catch risky dependencies before they enter the codebase.
Security News
As vulnerability data bottlenecks grow, the federal government is formally investigating NIST’s handling of the National Vulnerability Database.
Research
Security News
Socket’s Threat Research Team has uncovered 60 npm packages using post-install scripts to silently exfiltrate hostnames, IP addresses, DNS servers, and user directories to a Discord-controlled endpoint.