nodejs / nodejs/node

test_runner: MockTimers does not support Temporal

オープン
#63,369 コメント 5 件 リアクション 2 件 担当者 0 名 GitHub で見る

まだ誰も着手していません。

feature request test_runner
主要言語
JavaScript
スター
122k
フォーク
37.3k
平均マージ
4日 2時間
マージ済み PR(30日)
283

説明

What is the problem this feature solves?

As of Node.js 26, Temporal is enabled by default (#62526), and the platform is in the process of growing Temporal support across its APIs (#57891, #63154, #63312). One gap that doesn't seem to be tracked anywhere yet: node:test's MockTimers has no way to control Temporal.Now.

MockTimers.enable({ apis }) accepts 'setTimeout' | 'setInterval' | 'setImmediate' | 'Date'. Passing 'Temporal' (or any Temporal-related token) throws ERR_INVALID_ARG_VALUE. There is also no documented way to advance / freeze Temporal.Now.instant(), Temporal.Now.zonedDateTimeISO(), etc. via the mock clock.

Concretely, this means any code that reads "now" via Temporal.Now instead of Date.now() can't be tested deterministically with the built-in test runner. With Temporal now being the recommended way to do date/time work, this is increasingly the common case — code migrating away from Date loses its ability to use MockTimers for time-based assertions.

Repro
import { mock } from "node:test";

mock.timers.enable({ apis: ["Temporal"] });
// TypeError [ERR_INVALID_ARG_VALUE]: The property 'options.apis' must be one of:
// ['setTimeout', 'setImmediate', 'setInterval', 'Date']

mock.timers.enable({ apis: ["Date"], now: 0 });
console.log(new Date().toISOString());                 // 1970-01-01T00:00:00.000Z (mocked)
console.log(Temporal.Now.instant().toString());        // current real time (NOT mocked)
What is the feature you are proposing to solve the problem?

Extend MockTimers so that, when Temporal is in the apis list, the following are tied to the mock clock:

  • Temporal.Now.instant()
  • Temporal.Now.zonedDateTimeISO(timeZone?)
  • Temporal.Now.plainDateTimeISO(timeZone?)
  • Temporal.Now.plainDateISO(timeZone?)
  • Temporal.Now.plainTimeISO(timeZone?)
  • Temporal.Now.timeZoneId() (probably leave passthrough; only the clock should be virtual)

mock.timers.tick(ms) and mock.timers.setTime(ms) should advance Temporal's view of "now" the same way they advance Date.now(). The natural ergonomic is for 'Date' and 'Temporal' to be independently selectable but to share one underlying mock clock (so a test can mock both and have them agree).

This likely depends on #63312 (internal Temporal utils) landing first, since MockTimers would need a way to construct Temporal.Instants from the mock epoch ms without round-tripping through Date.

What alternatives have you considered?
  • Userland: wrap Temporal.Now in a project-level abstraction that reads from a clock the tests can mock. Works but is viral — every consumer has to use the wrapper.
  • Userland: monkey-patch Temporal.Now in test setup. Brittle and doesn't compose with mock.timers.
  • @sinonjs/fake-timers has the same gap upstream, so dropping the built-in runner doesn't help.

Filing per discussion in nodejs/node#57891 (which explicitly scopes out test_runner/generic support) — happy to take this on if there's interest and #63312 looks close to landing.

コントリビューションガイド

コントリビューションガイドを開く

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

調査の方向性

node:test MockTimers API から始め、issue の例を使って文書化されている Temporal の失敗を再現します。issue #63312 への依存関係を確認し、Temporal.Now が Date とモッククロックを共有する方法を判断します。説明どおりに MockTimers を通じて Temporal を選択、進行、凍結でき、記載されている Temporal.Now メソッドが決定論的に動作すれば完了です。

索引モデルが issue の本文から書いたものです。

評価

技術スタック
javascript
領域
testing-qa
issue の種類
機能追加
難易度
4/5
見積もり時間
3〜5日
活発さ
静か
明瞭さ
おおむね明確
初心者へのやさしさ
48/100

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。