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 🙏

© 2025 – Pkg Stats / Ryan Hefner

timewarp-sim

v1.4.1

Published

🕰️ Deterministic time simulation and manipulation library for TypeScript

Readme

timewarp-sim

🕰️ Deterministic Time Simulation and Manipulation Library for TypeScript

timewarp-sim lets you freeze, advance, and manipulate time in Node.js or TypeScript applications for deterministic testing, simulation, and debugging.

Built with a clean, predictable API and 100% TypeScript typings.


✨ Features

  • Freeze Time Globally: Lock the clock to a specific instant.
  • Advance Time: Simulate the passage of time step by step.
  • Travel to Any Timestamp: Jump forward or backward in time.
  • Return to Real Time: Resume normal clock behavior.
  • Mock Global Date: Make Date.now() and new Date() return simulated time.
  • Register Hooks: Get notified when the simulated time changes.
  • Zero Dependencies: Lightweight and minimal footprint.
  • Fully Typed: Rich TypeScript support out of the box.

📦 Installation

Install via npm:

npm install timewarp-sim

or yarn:

yarn add timewarp-sim

🚀 Quick Start Example

import { Timewarp } from "timewarp-sim";

// Show the current real time
console.log("Real time:", Timewarp.now());

// Freeze the clock
Timewarp.freeze();
console.log("Frozen:", Timewarp.now());

// Enable global Date mocking
Timewarp.enableGlobalMocking();

console.log("Mocked Date.now():", Date.now());
console.log("Mocked new Date():", new Date());
console.log("Mocked Date():", Date());

// Advance 1 hour
Timewarp.advance(1000 * 60 * 60);
console.log("After advancing 1 hour:", new Date());

// Travel to a specific date
Timewarp.travelTo(new Date("2040-01-01T00:00:00Z"));
console.log("Time traveled:", new Date());

// Resume real time
Timewarp.unfreeze();
Timewarp.disableGlobalMocking();

console.log("Back to real time:", new Date());

🧩 API Reference

Time Control

Timewarp.now(): Date

Returns the current simulated time.

Timewarp.freeze(): void

Freezes time to the current instant.

Timewarp.unfreeze(): void

Resumes real-time progression.

Timewarp.advance(ms: number): void

Advances frozen time by ms milliseconds.

Timewarp.travelTo(date: Date): void

Sets frozen time to a specific date.

Timewarp.isFrozen(): boolean

Returns true if time is currently frozen.


Hooks

Timewarp.onTimeChange(callback: (newTime: number) => void): void

Registers a hook triggered when time changes.

Timewarp.removeTimeChangeListener(callback): void

Removes a previously registered hook.


Global Mocking

Timewarp.enableGlobalMocking(): void

Overrides Date.now(), new Date(), and Date() globally to return simulated time.

✅ Example:

Timewarp.freeze();
Timewarp.enableGlobalMocking();

console.log(Date.now()); // frozen timestamp
console.log(new Date()); // frozen date object
console.log(Date());     // frozen date string

Timewarp.disableGlobalMocking(): void

Restores the original global Date object.

✅ Example:

Timewarp.disableGlobalMocking();
console.log(Date.now()); // real timestamp again

Timewarp.isGlobalMockingEnabled(): boolean

Returns true if global mocking is active.


🛠️ Use Cases

✅ Simulating token expiration scenarios. ✅ Testing time-sensitive workflows and scheduled jobs. ✅ Reproducing tricky date-related bugs. ✅ Advancing time deterministically during unit tests. ✅ Creating simulation environments for caching and TTL. ✅ Running integration tests with Date globally mocked.


🙏 Credits & Inspiration

This library was inspired by:

Developed with ❤️ by Omri Beladev.


📝 License

MIT License.


💡 Tips

  • Always disable global mocking after tests to avoid side effects.
  • Combine travelTo() and advance() for precise time manipulation.
  • Use hooks to log or trigger actions whenever simulated time changes.
  • If you only need deterministic time in specific parts of your app, prefer Timewarp.now() instead of enabling global mocking.

✨ Stay Connected

If you build something cool with timewarp-sim, share it or open a pull request!

Happy time traveling. ⏳