7 Best Time Mocking Tools for Tests in 2026: Control Clocks and Kill Flaky Sleeps
Compare 7 time mocking tools for tests in 2026, across JavaScript, Ruby, Python, and Kotlin, to replace sleep-based waits with controlled clocks.
On this page
Key takeaways
- Sleep-based waits make tests slow and flaky; clock control makes time deterministic.
- Pick a library that matches your language and test runner, not the other way around.
- Some tools mock time only, others bundle it into a full test framework.
- Check how each tool handles timers, time zones, and code that reads the system clock directly.
- Keep time mocking close to the code under test so failures stay easy to read.
What time mocking tools are for
These tools let a test take control of the clock. Instead of waiting for real seconds to pass, a test can freeze time, jump forward, or step through scheduled work on demand. That turns a slow, timing-dependent check into a fast, repeatable one. They are used most often for code with timeouts, retries, scheduled jobs, cache expiry, token lifetimes, and date formatting.
You need one when your suite contains sleeps, retry loops, or assertions that only pass on a fast machine. You probably do not need one for pure functions that take a timestamp as an argument, since you can pass a fixed value directly. Reach for a clock library when the code calls the system clock itself and you cannot inject a value without changing the design.
Quick comparison
| Tool | Best for | Starting price | Free plan |
|---|---|---|---|
| Sinon.JS | JavaScript clock and timer control | Not included | Not included |
| Timecop | Ruby time freezing | Not included | Not included |
| freezegun | Python datetime control | Not included | Not included |
| time-machine | Python clock travel with broad coverage | Not included | Not included |
| Jest | Bundled fake timers in a test runner | Not included | Not included |
| Vitest | Fast fake timers in a Vite-native runner | Not included | Not included |
| MockK | Kotlin mocking with time control | Not included | Not included |
The best time mocking tools for tests
1. Sinon.JS
- Official site: https://sinonjs.org
- Best for: JavaScript clock and timer control
- Starting price: Not included
- Free plan: Not included
- Pros: Mature fake clock and timer API; works with many JavaScript test runners
- Cons: Time control is one part of a larger library; setup needs care in some environments
Sinon.JS is a JavaScript testing library with stubs, spies, mocks, and a fake clock. Its clock feature lets a test replace the global timers and the current date, then advance time by hand. You can tick the clock forward to fire timeouts and intervals, or jump to a specific moment. It is used in unit tests and in browser-based test setups where real waiting would slow things down.
This fits teams already writing JavaScript tests who want time control without adopting a full framework. It differs from runner-bundled timers because it is a standalone library you add to any setup. The honest limit is that the fake clock only covers code that reads the timers and date it replaces, so code holding its own references needs attention.
2. Timecop
- Official site: https://github.com/travisjeffery/timecop
- Best for: Ruby time freezing
- Starting price: Not included
- Free plan: Not included
- Pros: Simple freeze and travel calls; familiar to Ruby developers
- Cons: Aimed at Ruby test suites only; less useful outside that ecosystem
Timecop is a Ruby library for controlling time in tests. It offers ways to freeze the clock at a moment, travel to another moment, and scale time so it moves faster or slower. Tests wrap the block of code they want to control, and time returns to normal afterward. It is a common choice for Ruby projects that check expiry, scheduling, or date logic.
Choose it when your suite is Ruby and you want a small, focused helper rather than a broad mocking framework. It differs from Python and JavaScript options because it plugs into Ruby test conventions directly. The limit is scope: it is a time helper for Ruby, so mixed-language projects still need separate tools for other runtimes.
3. freezegun
- Official site: https://github.com/spulec/freezegun
- Best for: Python datetime control
- Starting price: Not included
- Free plan: Not included
- Pros: Decorator and context manager usage; patches common date and time modules
- Cons: Can miss third-party code that reads the clock its own way; patch scope needs checking
freezegun is a Python library that freezes time for tests. You apply it as a decorator or use it as a context manager, and code that asks for the current date or time receives the frozen value. It also supports moving time forward within a test. This makes it handy for checking token expiry, cache lifetimes, and date math without real waiting.
It suits Python teams that want a direct way to pin the clock around a block of code. Compared with broader mocking libraries, it focuses on date and time rather than general objects. The honest limit is coverage: libraries that grab the clock through their own bindings may need extra care, so verify the code paths you depend on.
4. time-machine
- Official site: https://github.com/adamchainz/time-machine
- Best for: Python clock travel with broad coverage
- Starting price: Not included
- Free plan: Not included
- Pros: Travel and freeze helpers in one API; designed to cover many clock sources
- Cons: Python only; very broad patching can surprise code that expects real time
time-machine is a Python library for traveling through time in tests. It provides a travel function and a freeze helper, letting a test move the clock to a chosen moment and restore it afterward. It is built to intercept the common ways Python code reads the current time, which helps when several libraries are involved in one test.
This fits Python projects that want time control to reach deeper than a single module. It differs from freezegun in its emphasis on broad interception rather than a patch list. The limit is that wide-reaching control can mask code that should receive an injected clock, so treat it as a test aid, not a design shortcut.
5. Jest
- Official site: https://jestjs.io
- Best for: Bundled fake timers in a test runner
- Starting price: Not included
- Free plan: Not included
- Pros: Fake timers built into the runner; consistent API across a suite
- Cons: Tied to the Jest runner; timer behavior can differ from real scheduling
Jest is a JavaScript test runner with a built-in set of fake timer controls. Tests can switch to fake timers, advance them by a set amount, or run all pending timers at once. It also offers modern timer behavior that can cover date and scheduling APIs. For projects already on Jest, this removes the need to add a separate clock library.
It fits teams standardizing on one runner and wanting time control available everywhere. It differs from standalone libraries because the clock ships with the framework. The limit is that fake timer behavior is not identical to real scheduling, so code with deep timer assumptions may still need targeted checks.
6. Vitest
- Official site: https://vitest.dev
- Best for: Fast fake timers in a Vite-native runner
- Starting price: Not included
- Free plan: Not included
- Pros: Fake timers integrated with a fast runner; familiar timer API for JavaScript developers
- Cons: Best fit for Vite-based projects; timer edge cases still need review
Vitest is a JavaScript test runner built around the Vite toolchain, and it includes fake timer support. Tests can enable fake timers, advance them, and restore real time afterward. The timer API will feel familiar to developers who have used similar controls in other JavaScript runners, which keeps migration work low.
Choose it when your project already uses Vite and you want clock control without another dependency. It differs from Sinon.JS because the timers come from the runner rather than a mocking library. The honest limit is ecosystem fit: projects outside the Vite world may find another runner or a standalone clock library simpler.
7. MockK
- Official site: https://mockk.io
- Best for: Kotlin mocking with time control
- Starting price: Not included
- Free plan: Not included
- Pros: Kotlin-focused mocking with coroutine support; can verify time-related calls
- Cons: Kotlin only; mocking setup adds a learning curve
MockK is a mocking library for Kotlin. It covers stubs, spies, verification, and support for coroutines, which matters for tests that involve suspending work and delays. Time control usually comes from mocking the clock or the time source your code depends on, so tests can assert on scheduled behavior without real waiting.
This fits Kotlin projects, especially those using coroutines, where a general mocking tool is already needed. It differs from dedicated clock libraries because time control is one use of a broader mocking API. The limit is that you must mock the right abstraction, and a poorly chosen time source makes tests awkward.
How to choose
Start with your language and runner. A library that matches your stack will need less glue than one bolted on from another ecosystem. If your runner already ships fake timers, try those first before adding a dependency.
- Check what the tool replaces: global timers, date APIs, or both.
- Confirm support for coroutines, async code, or threads if your tests use them.
- Prefer tools that restore real time cleanly after each test.
- Consider time zones and locale-sensitive date logic.
- Weigh lock-in: a runner feature is convenient but ties you to that runner.
- Read the docs for known limits before relying on clock control in critical paths.
How we chose
Tools were selected from official product pages and documentation for fit, maintenance, and clear use cases. The list covers several languages and runtimes so readers can match a tool to their stack. Selection favored tools with documented time or clock control and an obvious role in automated tests. No documentation review was performed, and the descriptions stick to capabilities the vendor documentation states in general terms.
FAQ
What is time mocking in tests?
It is a technique where a test replaces the real clock with a controlled one. The test can then freeze time, jump forward, or run scheduled work on demand. This removes real waiting and makes timing-dependent checks repeatable.
Do I still need sleeps if I mock time?
Usually not for code that reads the mocked clock. You advance the fake clock instead of waiting. Some integration paths may still need real time, so keep those tests separate and few.
Can one tool cover every language in my project?
No. Most of these tools target a single language or runner. Mixed projects typically use one tool per runtime, with a shared convention for how tests control time.
Is a runner's built-in fake timer enough?
It often is when your whole suite uses that runner. A standalone library helps when you need clock control outside the runner or want one API across different setups.