npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

fake-date

v1.0.1

Published

util for mocking the Date API

Downloads

1

Readme

fake-date

npm install fake-date

Deterministic dates for JavaScript tests.

JavaScript Date API is non-deterministic in a couple of ways.

  1. Date.now() and new Date() varies depending on current time.
  2. Non-UTC input/output varies depending on the host machine's time zone. For example, 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.

How it Works

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.

API

Class Factory 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 Methods

The 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.

Caveats and Questions

  • Intended for testing, not production.
  • It does not override the global 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.
  • Performance will be slightly worse than the native Date API.
  • It's timezone-offset-aware, but not timezone-aware. Timezone offset doesn't tell you everything you need to know to determine which timezone you're in. Thus, 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)".
  • This module totally punts on toLocaleString(), toLocaleDateString(), and toLocaleTimeString(), which return the exact same values as toString(), toDateString(), and toTimeString().