Skip to content

Restore Date object support broken by PR #556#561

Merged
fatso83 merged 1 commit intosinonjs:mainfrom
adspacheco:fix/cross-realm-date-setSystemTime
Apr 10, 2026
Merged

Restore Date object support broken by PR #556#561
fatso83 merged 1 commit intosinonjs:mainfrom
adspacheco:fix/cross-realm-date-setSystemTime

Conversation

@adspacheco
Copy link
Copy Markdown
Contributor

Purpose (TL;DR) - mandatory

Fix cross-realm Date regression introduced in #556.

Background (Problem in detail)

The public API accepts Date | number, but the change in #556 from typeof epoch.getTime === "function" to instanceof Date broke support for Date objects from different realms (iframes, vm contexts, etc.), since instanceof fails across realms.

Error: TypeError: now should be milliseconds since UNIX epoch

Reproduction:

const vm = require("vm");                                                                                                                 
const FakeTimers = require("@sinonjs/fake-timers");                                                                                       
const crossRealmDate = vm.runInNewContext("new Date('2026-04-02')");                                                                      
const clock = FakeTimers.install();                                                                                                       
clock.setSystemTime(crossRealmDate); // throws TypeError                                                                                  

Solution

Restore the original duck-typing approach (typeof epoch.getTime === "function") that worked before #556.

The change from `typeof epoch.getTime === "function"` to
`instanceof Date` in #556 broke cross-realm Date support.

When Jest runs tests in a different realm/context, Date objects
created there fail the `instanceof Date` check because each
realm has its own Date constructor.

This restores the original duck-typing approach that works
across realms.

Fixes the error: "TypeError: now should be milliseconds since UNIX epoch"
@fatso83 fatso83 merged commit 1f6c47c into sinonjs:main Apr 10, 2026
11 checks passed
fatso83 added a commit that referenced this pull request Apr 10, 2026
@fatso83
Copy link
Copy Markdown
Contributor

fatso83 commented Apr 10, 2026

added the missing regression test in aa15fde for you, as I introduced the regression 😉

@adspacheco adspacheco deleted the fix/cross-realm-date-setSystemTime branch April 10, 2026 17:28
@fatso83
Copy link
Copy Markdown
Contributor

fatso83 commented Apr 10, 2026

Unfortunately, this fix was incomplete, as the typing now breaks. This is what I get for not ensuring the types are built as part of the github actions. At least I know that now 😄

src/fake-timers-src.js:642:26 - error TS2339: Property 'getTime' does not exist on type 'number | Date'.
  Property 'getTime' does not exist on type 'number'.

642         if (typeof epoch.getTime === "function") {
                             ~~~~~~~

src/fake-timers-src.js:643:26 - error TS2339: Property 'getTime' does not exist on type 'number | Date'.
  Property 'getTime' does not exist on type 'number'.

643             return epoch.getTime();
                             ~~~~~~~


Found 2 errors in the same file, starting at: src/fake-timers-src.js:642

I'll get it fixed.

@fatso83
Copy link
Copy Markdown
Contributor

fatso83 commented Apr 10, 2026

types are now always built and the type regression fixed in d43b719

release 15.3.1 out in a few jiffies

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants