Skip to content

date instanceof Date returns false for dates created before vi.setSystemTime() #9034

Closed

Description

@diego-aquino

Describe the bug

After calling vi.setSystemTime(), date instanceof Date appears to return true only if the date was created after the call. If the date was created before vi.setSystemTime(), the result is false.

Is this expected?

Reproduction

https://github.com/diego-aquino/vitest-set-system-time-instanceof

  1. pnpm install
  2. pnpm test

example.test.ts contains a test reproducing the issue:

it('should preserve date instanceof after changing the system time', async () => {
  const dateBeforeSettingSystemTime = new Date('2025-11-15T08:00:00.000Z');
  expect(dateBeforeSettingSystemTime).toBeInstanceOf(Date); // ok

  vi.setSystemTime(dateBeforeSettingSystemTime);

  const dateAfterSettingSystemTime = new Date();
  expect(dateAfterSettingSystemTime).toEqual(dateBeforeSettingSystemTime); // system time set

  expect(dateAfterSettingSystemTime).toBeInstanceOf(Date); // ok
  expect(dateBeforeSettingSystemTime).toBeInstanceOf(Date); // fails?
});
 FAIL  example.test.ts > should preserve date instanceof after changing the system time
AssertionError: expected 2025-11-15T08:00:00.000Z to be an instance of MockDate
 ❯ example.test.ts:13:39
     11| 
     12|   expect(dateAfterSettingSystemTime).toBeInstanceOf(Date);
     13|   expect(dateBeforeSettingSystemTime).toBeInstanceOf(Date);
       |                                       ^
     14| });
     15| 

System Info

System:
    OS: Linux 6.8 Ubuntu 24.04.3 LTS 24.04.3 LTS (Noble Numbat)
    CPU: (12) x64 Intel(R) Core(TM) i5-10400F CPU @ 2.90GHz
    Memory: 5.76 GB / 15.54 GB
    Container: Yes
    Shell: 5.9 - /usr/bin/zsh
  Binaries:
    Node: 24.11.1 - /usr/bin/node
    Yarn: 1.22.22 - /home/diegoaquino/.n/bin/yarn
    npm: 11.6.2 - /home/diegoaquino/.n/bin/npm
    pnpm: 10.13.1 - /home/diegoaquino/.n/bin/pnpm
    bun: 1.3.0 - /home/diegoaquino/.bun/bin/bun
  Browsers:
    Chrome: 142.0.7444.162
    Firefox: 145.0
    Firefox Developer Edition: 145.0
  npmPackages:
    vitest: ^4.0.9 => 4.0.9
``

Used Package Manager

pnpm

Validations

Activity

  1. added theissue type on Nov 15, 2025
  2. Prajwal2246 commented on Oct 8, 2026

    @Prajwal2246
    Contributor

    This looks fixed in v5. I ran the repro on a few versions:

    • 4.0.9, 4.0.18, 4.1.0, 4.1.11: fails ("expected ... to be an instance of MockDate")
    • 5.0.0 and 5.0.3: passes

    I think #10757 fixed it: setSystemTime without fake timers doesn't use the old MockDate anymore, it uses fake-timers' clock Date, and dates created before the call are still instanceof Date. Also passes with vi.useFakeTimers() first and after vi.useRealTimers().

    So this can probably be closed as fixed in 5.0.0.

  3. locked and limited conversation to collaborators on Oct 23, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions