Skip to content

vi.useFakeTimers() doesn't fake performance by default despite docs claiming it does #9352

@a-non-a-mouse

Description

@a-non-a-mouse

Documentation is

  • Missing
  • Outdated
  • Confusing
  • Not sure?

Explain in Detail

Docs state that toFake defaults to "everything available globally except nextTick and queueMicrotask", which should include performance.

However, performance.now() is not faked unless explicitly specified:

import { beforeEach, expect, it, vi } from 'vitest';

beforeEach(() => {
  vi.useFakeTimers();
});

it('fails - performance.now not advancing', () => {
  const start = performance.now();
  vi.advanceTimersByTime(100);
  expect(performance.now() - start).toBe(100); // fails: expected 100, got 0.1....
});

Requires:

vi.useFakeTimers({ toFake: ['performance'] });

Similarly, https://vitest.dev/api/vi states:

If fake timers are enabled, this method simulates a user changing the system clock (will affect date related API like hrtime, performance.now or new Date())

and

vi.useFakeTimers() does not automatically mock process.nextTick and queueMicrotask. But you can enable it by specifying the option in toFake argument: vi.useFakeTimers({ toFake: ['nextTick', 'queueMicrotask'] }).

Which appear to be wrong / incomplete

Your Suggestion for Changes

Update docs to remove performance, possibly others, from list of default mocked timers

Reproduction

No response

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions