Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

getMockedSystemTime() returns null if the fake time is specified using useFakeTimer({now: fakeTime}) #7329

Open
6 tasks done
xuhdev opened this issue Jan 22, 2025 · 1 comment
Labels
p3-minor-bug An edge case that only affects very specific usage (priority)

Comments

@xuhdev
Copy link
Contributor

xuhdev commented Jan 22, 2025

Describe the bug

getMockedSystemTime() returns null if the fake time is specified using useFakeTimer({now: fakeTime}), while it correctly returns the mocked system time if using setSystemTime().

Reproduction

Run vitest with the following test:

import {vi, test, expect} from "vitest";

test("Speicify with now", () => {
    vi.useFakeTimers({
        now: 333
    });
    expect(vi.getMockedSystemTime()).toBeNull();  // <---- Shouldn't be null
})

test("Speicify with setSystemTime", () => {
    vi.useFakeTimers();
    vi.setSystemTime(333);
    expect(vi.getMockedSystemTime()?.getTime()).toBe(333);
})

The test will pass, but the first test shouldn't pass.

System Info

System:
    OS: Linux 6.12 Fedora Linux 41 (Workstation Edition)
    CPU: (12) x64 AMD Ryzen 5 8400F 6-Core Processor
    Memory: 56.15 GB / 62.40 GB
    Container: Yes
    Shell: 5.9 - /bin/zsh
  Binaries:
    Node: 22.13.0 - ~/.nvm/versions/node/v22.13.0/bin/node
    Yarn: 1.22.22 - ~/.nvm/versions/node/v22.13.0/bin/yarn
    npm: 11.0.0 - ~/.nvm/versions/node/v22.13.0/bin/npm
    pnpm: 9.14.2 - ~/.nvm/versions/node/v22.13.0/bin/pnpm
  npmPackages:
    vitest: ^3.0.3 => 3.0.3

Used Package Manager

npm

Validations

@hi-ogawa hi-ogawa added p3-minor-bug An edge case that only affects very specific usage (priority) and removed pending triage labels Jan 22, 2025
@hi-ogawa
Copy link
Contributor

I created a stackblitz repro https://stackblitz.com/edit/vitest-dev-vitest-z1nfegqo?file=test%2Fbasic.test.ts

Though documentation explicitly says setSystemTime, I think it makes sense to consider { now: ... } options too. https://vitest.dev/api/vi.html#vi-getmockedsystemtime

Returns mocked current date that was set using setSystemTime.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
p3-minor-bug An edge case that only affects very specific usage (priority)
Projects
None yet
Development

No branches or pull requests

2 participants