fake-current-time
v0.3.1
Published
Manipulate current time in your application for development and staging environments
Maintainers
Readme
fake-current-time
Manipulate current time in your application for development and staging environments.
Uses Proxy and AsyncLocalStorage to isolate time contexts per request, allowing multiple users to test with different time offsets simultaneously.
Installation
npm install fake-current-timeUsage
This example shows integration with React Router framework mode. The key concept is:
- Server: Parse offset from cookie and wrap request handling with
runner.run(offset, ...) - Client: Call
setup()to initialize time manipulation - UI: Use
setOffset()andclearOffset()to control time via cookie
Server Entry (Express with React Router)
import express from "express";
import { createRequestHandler } from "@react-router/express";
import { setup, parseOffsetFromCookie } from "fake-current-time/node";
const app = express();
// Do NOT setup in production
const runner = process.env.STAGE !== "production" ? setup() : null;
app.all(
"*",
(req, _, next) => {
const offset = runner
? parseOffsetFromCookie(req.headers.cookie || "")
: undefined;
return offset && runner ? runner.run(offset, next) : next();
},
createRequestHandler({
// ...
}),
);Client Entry (entry.client.tsx)
import { startTransition } from "react";
import { setup } from "fake-current-time/browser";
// Do NOT setup in production
if (process.env.STAGE !== "production") {
setup();
}
startTransition(() => {
// ...
});UI Component
import { FakeTimeController } from "fake-current-time/react";
// Restrict access to this page in production (e.g., via routing or middleware)
export function TimeControlPage() {
return <FakeTimeController />;
}
Accessing the Original Date
If you need to access the real Date while time manipulation is active, use OriginalDate:
import { OriginalDate } from "fake-current-time";
const realNow = OriginalDate.now();
const realDate = new OriginalDate();