test_runner: MockTimers does not support Temporal
还没有人认领这个 Issue。
评估
- 难度
- 4/5
- 预计耗时
- 3-5 天
- 新手友好度
- 48/100
- Issue 类型
- 功能
- 描述清晰度
- 基本清楚
- 活跃度
- 冷清
- 技术栈
- javascript
- 领域
- testing-qa
调研方向
从 node:test MockTimers API 开始,使用 issue 中的示例重现文档所述的 Temporal 失败。检查对 issue #63312 的依赖,并确定 Temporal.Now 应如何与 Date 共享模拟时钟。完成的标准是:如描述所述,可以通过 MockTimers 选择、推进和冻结 Temporal,并且列出的 Temporal.Now 方法能够以确定性方式运行。
由索引模型根据 Issue 内容生成。
描述
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.Nowin 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.Nowin test setup. Brittle and doesn't compose withmock.timers. @sinonjs/fake-timershas 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.
- 主要语言
- JavaScript
- 星标
- 122k
- 派生
- 37.4k
- 平均合并
- 4 天 3 小时
- 30 天内合并 PR
- 279
贡献指南
从这里开始
- 先读完整个 Issue,再读项目的贡献指南。
- 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
- Fork 仓库,在一个分支上完成修改。
- 提交 Pull Request,并在描述里引用这个 Issue 编号。
nodejs/node 的其他 Issue
-
doc
难度 2/5 1-3 小时 新手友好度 65/100
-
build
难度 1/5 1 小时以内 新手友好度 88/100
-
难度 2/5 1-3 小时 新手友好度 84/100
-
难度 1/5 1 小时以内 新手友好度 90/100
-
feature request
难度 2/5 1-3 小时 新手友好度 68/100
相似的 Issue
-
难度 2/5 1-3 小时 新手友好度 65/100
-
难度 2/5 1-3 小时 新手友好度 75/100
docToolchain/docToolchain#1705 ·
-
Timezone select lists one option per character; duplicate "Failed" reason; shared tracker popover id 未关闭bug
难度 2/5 1-3 小时 新手友好度 75/100
nightscout/nocturne#1414 ·
-
bug v2
难度 2/5 1-3 小时 新手友好度 75/100
modelcontextprotocol/inspector#2458 · 1 条评论 ·
-
Mend: dependency security vulnerability status: needs triage 🕵️♀️
难度 2/5 1-3 小时 新手友好度 70/100
carbon-design-system/ibm-products#9907 ·