
Security News
Opengrep Adds Apex Support and New Rule Controls in Latest Updates
The latest Opengrep releases add Apex scanning, precision rule tuning, and performance gains for open source static code analysis.
npm install fake-date
Deterministic dates for JavaScript tests.
JavaScript Date
API is non-deterministic in a couple of ways.
Date.now()
and new Date()
varies depending on current time.new Date(2011, 0, 1)
represents when Jan 1, 2011 started in your time zone, and someDate.getMonth()
returns the month it was/is/will be for that date in your timezone.Thus, the same code might have different results on different runs across time and space. That's great sometimes, but bad for unit tests. fake-date replicates the Date
API, but in a deterministic way, in order to be able to write unit tests that pass or fail consistently.
This module exports a class factory which accepts options and returns a FakeDate
class which is locked to a specific reference time and timezone offset of your choosing. You can swap in this FakeDate
class anywhere you'd normally use the global Date
class.
fakeDate()
const fakeDate = require('fake-date');
const FakeDate = fakeDate({
// An integer representing minutes, reflecting
// JS's Date#getTimezoneOffset() method
timezoneOffset: 120, // UTC-02:00
// Will be used for FakeDate.now()
// and new FakeDate()
referenceTime: 0,
});
console.log(new FakeDate().getTimezoneOffset()); // 120
console.log(new FakeDate(2000, 0, 1).toISOString()); // 2000-01-01T02:00:00.000Z
console.log(FakeDate.now()); // 0
setTimeout(() => console.log(FakeDate.now()), 100000); // 0
FakeDate
Static and Instance MethodsThe methods are the same as the Date
class. See the MDN docs for details. The difference is that the concepts of right now and timezone offset are fixed and controlled by you, rather than being runtime/host-machine-dependent.
Date
object for you. You can do that yourself if you want, or better yet, make your date-dependent code accept a date implementation which defaults to the global Date
object, but passing in a FakeDate
during tests.Date
API.toString()
and toTimeString()
include the time zone offset, but leave off the timezone name, for example "Sat Jan 01 2000 00:00:00 GMT-0700"
instead of "Sat Jan 01 2000 00:00:00 GMT-0700 (MST)"
.toLocaleString()
, toLocaleDateString()
, and toLocaleTimeString()
, which return the exact same values as toString()
, toDateString()
, and toTimeString()
.FAQs
util for mocking the Date API
The npm package fake-date receives a total of 2 weekly downloads. As such, fake-date popularity was classified as not popular.
We found that fake-date 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.
Security News
The latest Opengrep releases add Apex scanning, precision rule tuning, and performance gains for open source static code analysis.
Security News
npm now supports Trusted Publishing with OIDC, enabling secure package publishing directly from CI/CD workflows without relying on long-lived tokens.
Research
/Security News
A RubyGems malware campaign used 60 malicious packages posing as automation tools to steal credentials from social media and marketing tool users.