<?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 JavaScript / 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 almost nothing wrong with it until tests come.&lt;/p&gt;

&lt;p&gt;The result is you will 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 Sinon/Vitest's fake timers are great tools, they always felt like they were solving the problem from the outside by patching global APIs, which is not the greatest DDD pattern.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;What about treating time as a dependency ?&lt;/strong&gt;&lt;br&gt;
.NET is doing it since version 8.&lt;/p&gt;

&lt;p&gt;When you think about it, time isn't much different from a database or an HTTP client from a business logic : your code 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;&lt;code&gt;const now = new Date();&lt;/code&gt;&lt;/p&gt;

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

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

&lt;p&gt;Your code is now testable because you rely on a provider for your time.&lt;/p&gt;

&lt;p&gt;Why not a classic IClock interface then ? Because that could typically been factored out, precisely because it's always the same tiny logic here : abstract time reading behind an interface and inject a fake one in tests. That's what Time-Provider does.&lt;/p&gt;

&lt;p&gt;Actually it goes beyond this because :&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;You keep your favourite Date library (via plugging it through the plugin interface).&lt;/li&gt;
&lt;li&gt;You have timers (timeout/interval) abstractions at no costs (with 3 clocks strategies in deterministic mode) + a helper to chain and rearms setTimeout calls with a fluctuating delay (aka. dynamic intervals).&lt;/li&gt;
&lt;li&gt;Performance api is also there.&lt;/li&gt;
&lt;li&gt;And if you need the request animation frame API for browsers, it's also there through an add-on (because it's browser specific).&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;At this point you don't need global fake timers anymore. You just rely on the time-provider system pass-through for your production code and uses the deterministic version for your tests. And Time-Provider is great for this because it's tree-shakable : Your production code is never bloated with deterministic code.&lt;/p&gt;

&lt;p&gt;This is the purpose of @time-provider/core.&lt;/p&gt;

&lt;p&gt;Thanks to it's add-on architecture you can easily extend, with additional features. Cron and ETA are next to come.&lt;/p&gt;

&lt;p&gt;The result is you only import what you need. Your code stay small and testable.&lt;/p&gt;

&lt;p&gt;I'd love to hear how other JavaScript or 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/or alternative approaches are all welcome.&lt;/p&gt;

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