Skip to content

fake timer doesn't auto advance for browser mode actions #10058

Closed

Description

@radqut

Describe the bug

When using vi.useFakeTimers(), commands like .click do not work, but they do with real timers

Reproduction

// example.spec.tsx
import { useEffect, useState } from "react";
import { test, vi } from "vitest";
import { page } from "vitest/browser";
import { render } from "vitest-browser-react";

const TestComponent = () => {
  const [show, setShow] = useState(false);

  useEffect(() => {
    setTimeout(() => setShow(true), 1000);
  }, []);

  if (!show) {
    return null;
  }

  return <button>Click me!</button>;
};

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

  onTestFinished(() => vi.useRealTimers());

  await render(<TestComponent />);

  await page.getByRole("button").click();
});

With fake timers

 ❯  chromium  src/example.spec.tsx (1 test | 1 failed) 15012ms
   × failed with fake timers 15011ms

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

 FAIL   chromium  src/example.spec.tsx > failed with fake timers
TimeoutError: locator.click: Timeout 14888ms exceeded.
Call log:
  - waiting for locator('[data-vitest="true"]').contentFrame().getByRole('button')


Failure screenshot:
  - src/__screenshots__/example.spec.tsx/failed-with-fake-timers-1.png

 ❯ src/example.spec.tsx:27:33
     25|   await render(<TestComponent />);
     26|
     27|   await page.getByRole("button").click();
       |                                 ^
     28| });
     29|

⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯[1/1]⎯


 Test Files  1 failed (1)
      Tests  1 failed (1)
   Start at  21:59:18
   Duration  16.71s (transform 0ms, setup 0ms, import 100ms, tests 15.01s, environment 0ms)

Without fake timers

 ✓  chromium  src/example.spec.tsx (1 test) 1496ms
   ✓ failed with fake timers  1495ms

 Test Files  1 passed (1)
      Tests  1 passed (1)
   Start at  22:00:30
   Duration  3.12s (transform 0ms, setup 0ms, import 112ms, tests 1.50s, environment 0ms)

System Info

System:
    OS: Windows 11 10.0.26200
    CPU: (8) x64 Intel(R) Core(TM) i3-10100F CPU @ 3.60GHz
    Memory: 16.16 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.1 - 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
    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

Activity

  1. added theissue type on Apr 3, 2026
  2. hi-ogawa commented on Apr 4, 2026

    @hi-ogawa
    Collaborator

    Can you explain why this is an unexpected behavior? It looks to me fake timer stalling setTimeout and thus show never becomes true.

  3. radqut commented on Apr 4, 2026

    @radqut
    Author

    @hi-ogawa If we use a real timer, we don't need to check whether an element exists — we can just call .click directly. With a fake timer, however, we need to advance the timer and use vi.waitFor to find the element first, and only then call .click. This is a bit confusing. It would be great if the commands also advanced the fake timer.

  4. sheremet-va commented on Apr 4, 2026

    @sheremet-va
    Member

    @hi-ogawa If we use a real timer, we don't need to check whether an element exists — we can just call .click directly. With a fake timer, however, we need to advance the timer and use vi.waitFor to find the element first, and only then call .click. This is a bit confusing. It would be great if the commands also advanced the fake timer.

    You can call vi.setTimerTickMode(“nextTimerAsync”) to advance timers automatically.

  5. hi-ogawa commented on Apr 4, 2026

    @hi-ogawa
    Collaborator

    I'd also hope nextTimerAsync saving all the issues and we should suggest more, but I think one confusion might be expect.element, vi.waitFor/waitUntil auto advances, but click or actions don't. Probably currently it would work like:

    test("expect.element advances fake timer", async ({ onTestFinished }) => {
      vi.useFakeTimers();
      onTestFinished(() => vi.useRealTimers());
    
      await render(<TestComponent />);
      await expect.element(page.getByRole("button")).toBeVisible()
    });
    
    test("click doesn't advance fake timer", async ({ onTestFinished }) => {
      vi.useFakeTimers();
      onTestFinished(() => vi.useRealTimers());
    
      await render(<TestComponent />);
      await page.getByRole("button").click();
    });

    I think we should still handle this from documentation perspective.

  6. radqut commented on Apr 4, 2026

    @radqut
    Author

    @hi-ogawa If we use a real timer, we don't need to check whether an element exists — we can just call .click directly. With a fake timer, however, we need to advance the timer and use vi.waitFor to find the element first, and only then call .click. This is a bit confusing. It would be great if the commands also advanced the fake timer.

    You can call vi.setTimerTickMode(“nextTimerAsync”) to advance timers automatically.

    It does not work the same. Here is an example:

    import { useEffect, useState } from "react";
    import { test, vi, expect } from "vitest";
    import { page } from "vitest/browser";
    import { render } from "vitest-browser-react";
    
    const TestComponent = () => {
      const [show, setShow] = useState(false);
    
      useEffect(() => {
        setTimeout(() => setShow(true), 1000);
      }, []);
    
      useEffect(() => {
        if (show) {
          setTimeout(() => setShow(false), 1000);
        }
      }, [show]);
    
      if (!show) {
        return null;
      }
    
      return <button>Click me!</button>;
    };
    
    test("passed without fake timer", async () => {
      await render(<TestComponent />);
    
      await page.getByRole("button").click();
    });
    
    test("passed with expect.element", async ({ onTestFinished }) => {
      vi.useFakeTimers();
      onTestFinished(() => void vi.useRealTimers());
    
      await render(<TestComponent />);
    
      await expect.element(page.getByRole("button")).toBeVisible());
      await page.getByRole("button").click();
    });
    
    test("failed with 'setTimerTickMode'", async ({ onTestFinished }) => {
      vi.useFakeTimers();
      vi.setTimerTickMode("nextTimerAsync");
      onTestFinished(() => void vi.useRealTimers());
    
      await render(<TestComponent />);
    
      await page.getByRole("button").click();
    });
  7. changed the title [-]commands does not work with fake timers[/-] [+]fake timer doesn't auto advance for browser mode actions[/+] on Apr 5, 2026
  8. added 3 commits that reference this issue on Sep 11, 2026
    db7511b
    22309f2
    00b710d
  9. added
    p2-to-be-discussed
    on Sep 14, 2026
  10. moved this to P2 - 2 in Team Boardon Sep 14, 2026
  11. added
    p3-minor-bug
    and removed
    p2-to-be-discussed
    on Sep 25, 2026
  12. added 2 commits that reference this issue on Oct 5, 2026
    3a52559
    6b13230
  13. hi-ogawa commented on Oct 19, 2026

    @hi-ogawa
    Collaborator

    Just revisiting the setTimerTickMode("nextTimerAsync") repro above (#10058 (comment)). It fails because the example component flips hidden -> shown -> hidden through two setTimeouts, which leaves the button visible for a 1000ms window. nextTimerAsync runs those timers as quickly as possible, so in the test the button is visible for too short a moment for any action or assertion to catch it. expect.element(...).toBeVisible() fails the same way under nextTimerAsync, so it isn't specific to actions.

    Thinking about it more, this suggests nextTimerAsync isn't a universal auto-advance mechanism, and it doesn't fit testing chained timer behavior where some state only lasts for a while. Ideally, I think such a component would be tested reliably with precise manual advancing, for example await vi.advanceTimersByTimeAsync(1000) before the click. But implementing auto advance at the action and assertion level, the way expect.element already does, also seems reasonable, which is what #11242 explores.

  14. locked and limited conversation to collaborators on Nov 3, 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

    Labels

    p3-minor-bug

    Type

    Projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions