vitest-dev / vitest-dev/vitest

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

Open
#9,034 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

pending triage
Dominant language
TypeScript
Stars
17.1k
Forks
2k
Avg merge
1d 22h
Merged PRs (30d)
94

Description

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

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Run the linked reproduction with pnpm install and pnpm test, starting from example.test.ts. Trace the Date mocking behavior around vi.setSystemTime; done means the pre-existing Date passes toBeInstanceOf(Date) after system time changes while the reproduction test remains passing.

Written by the indexing model from the issue text.

Assessment

Tech stack
typescript
Domain
testing-qa
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
42/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.