<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: jaenyf</title>
    <description>The latest articles on DEV Community by jaenyf (@jaenyf).</description>
    <link>https://dev.to/jaenyf</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F4063106%2Fa14a7040-e7b5-45c6-a4df-38802e96f427.png</url>
      <title>DEV Community: jaenyf</title>
      <link>https://dev.to/jaenyf</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/jaenyf"/>
    <language>en</language>
    <item>
      <title>I got tired of mocking Date, so I built a TimeProvider for TypeScript</title>
      <dc:creator>jaenyf</dc:creator>
      <pubDate>Tue, 04 Aug 2026 21:03:11 +0000</pubDate>
      <link>https://dev.to/jaenyf/i-got-tired-of-mocking-date-so-i-built-a-timeprovider-for-typescript-41mm</link>
      <guid>https://dev.to/jaenyf/i-got-tired-of-mocking-date-so-i-built-a-timeprovider-for-typescript-41mm</guid>
      <description>&lt;p&gt;Every (or at least a lot of) project seems to have code like this somewhere:&lt;/p&gt;

&lt;p&gt;if (user.subscriptionEndsAt &amp;lt; new Date()) {&lt;br&gt;
    // ...&lt;br&gt;
}&lt;/p&gt;

&lt;p&gt;There's nothing wrong with it... until you have to test it.&lt;/p&gt;

&lt;p&gt;Then you end up freezing time, mocking Date, enabling fake timers, remembering to restore them afterwards, and hoping another test didn't leave the clock in a weird state.&lt;/p&gt;

&lt;p&gt;While Jest's and Vitest's fake timers are great tools, they always felt like they were solving the problem from the outside by patching global APIs.&lt;/p&gt;

&lt;p&gt;I wanted to try something different.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Time is a dependency&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;When you think about it, the current time isn't much different from a database or an HTTP client.&lt;/p&gt;

&lt;p&gt;Your business logic depends on it, but it doesn't have to know where it comes from.&lt;/p&gt;

&lt;p&gt;Instead of writing this:&lt;/p&gt;

&lt;p&gt;const now = new Date();&lt;/p&gt;

&lt;p&gt;what if we wrote this?&lt;/p&gt;

&lt;p&gt;const now = timeProvider.now();&lt;/p&gt;

&lt;p&gt;Suddenly, testing becomes boring—in the best possible way.&lt;/p&gt;

&lt;p&gt;You don't need global fake timers anymore. You just pass a different implementation.&lt;/p&gt;

&lt;p&gt;.NET had the same idea&lt;/p&gt;

&lt;p&gt;While looking into this, I discovered that .NET 8 introduced a TimeProvider abstraction.&lt;/p&gt;

&lt;p&gt;Seeing that was reassuring. It suggested I wasn't the only one who felt that "current time" deserved to be treated as a real dependency.&lt;/p&gt;

&lt;p&gt;I didn't want to copy the .NET API, but I did like the underlying idea.&lt;/p&gt;

&lt;p&gt;So I started building a version that felt natural in the TypeScript ecosystem.&lt;/p&gt;

&lt;p&gt;It grew beyond a clock&lt;/p&gt;

&lt;p&gt;At first I only wanted to replace new Date().&lt;/p&gt;

&lt;p&gt;Then I realized the same issue exists with setTimeout, setInterval, performance measurements, and a few other APIs.&lt;/p&gt;

&lt;p&gt;They all depend on the environment's notion of time.&lt;/p&gt;

&lt;p&gt;So the library slowly became an abstraction around all of those instead of just "what time is it?".&lt;/p&gt;

&lt;p&gt;Is this actually useful?&lt;/p&gt;

&lt;p&gt;That's the part I'm still curious about.&lt;/p&gt;

&lt;p&gt;In the projects I've worked on, I prefer injecting time over patching globals during tests.&lt;/p&gt;

&lt;p&gt;Maybe other teams have reached the same conclusion.&lt;/p&gt;

&lt;p&gt;Maybe everyone is perfectly happy with fake timers and I'm overengineering things.&lt;/p&gt;

&lt;p&gt;Either way, I'd love to hear how other TypeScript developers approach this.&lt;/p&gt;

&lt;p&gt;If you're interested, the project is here:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://jaenyf.github.io/time-provider/" rel="noopener noreferrer"&gt;https://jaenyf.github.io/time-provider/&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Feedback, criticism, and alternative approaches are all welcome.&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>typescript</category>
      <category>npm</category>
      <category>node</category>
    </item>
  </channel>
</rss>
