Skip to content

expect.poll does not work with fake timers #10020

@radqut

Description

@radqut

Describe the bug

When using vi.useFakeTimers(), expect.poll does not work. I believe it should handle this by internally calling vi.advanceTimersByTime(interval), as vi.waitFor does.

Reproduction

// example.spec.ts
import { expect, test, vi } from "vitest";

test("expect.poll with fake timers", async () => {
  vi.useFakeTimers();

  let count = 0;

  setTimeout(() => {
    count = 1;
  }, 1000);

  await expect.poll(() => count, { timeout: 2000 }).toBe(1);

  vi.useRealTimers();
});

test("vi.waitFor with fake timers", async () => {
  vi.useFakeTimers();

  let count = 0;

  setTimeout(() => {
    count = 1;
  }, 1000);

  await vi.waitFor(() => expect(count).toBe(1), { timeout: 2000 });

  vi.useRealTimers();
});
❯  unit  src/example.spec.ts (2 tests | 1 failed) 3232ms
   × expect.poll with fake timers 2052ms
   ✓ vi.waitFor with fake timers  1178ms

⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯ Failed Tests 1 ⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯

 FAIL   unit  src/example.spec.ts > expect.poll with fake timers
AssertionError: expected +0 to be 1 // Object.is equality

- Expected
+ Received

- 1
+ 0

 ❯ src/example.spec.ts:12:53
     10|   }, 1000);
     11|
     12|   await expect.poll(() => count, { timeout: 2000 }).toBe(1);
       |                                                     ^
     13|
     14|   vi.useRealTimers();

Caused by: Error: Matcher did not succeed in time.
 ❯ src/example.spec.ts:12:3

System Info

System:
    OS: Windows 11 10.0.26200
    CPU: (8) x64 Intel(R) Core(TM) i3-10100F CPU @ 3.60GHz
    Memory: 15.31 GB / 31.93 GB
  Binaries:
    Node: 22.21.1 - C:\Users\Micha\AppData\Local\mise\installs\node\22.21.1\node.EXE
    Yarn: 1.22.22 - C:\Users\Micha\AppData\Local\mise\installs\node\22.21.1\yarn.CMD
    npm: 11.12.0 - C:\Users\Micha\AppData\Local\mise\installs\node\22.21.1\npm.CMD
    pnpm: 10.33.0 - C:\Users\Micha\AppData\Local\mise\installs\node\22.21.1\pnpm.CMD
  Browsers:
    Chrome: 146.0.7680.165
    Edge: Chromium (142.0.3595.69)
  npmPackages:
    @vitejs/plugin-react: ^6.0.1 => 6.0.1
    @vitest/browser-playwright: ^4.1.2 => 4.1.2
    playwright: ^1.58.2 => 1.58.2
    vite: ^8.0.1 => 8.0.3
    vitest: ^4.1.2 => 4.1.2
    vitest-browser-react: ^2.1.0 => 2.1.0

Used Package Manager

pnpm

Validations

Metadata

Metadata

Assignees

No one assigned

    Labels

    p3-minor-bugAn edge case that only affects very specific usage (priority)

    Type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions