fake timer doesn't auto advance for browser mode actions

Open
#10,058 6 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

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

Research direction

Start by running the provided example.spec.tsx reproduction with the browser runner, fake timers, and the React component shown. Trace how browser-mode actions and the setTimeout-driven button appearance interact, then add a regression test covering the example; done means the button becomes available and click completes without the timeout.

Written by the indexing model from the issue text.

Description

p2-to-be-discussed pending triage
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
Dominant language
TypeScript
Stars
17.1k
Forks
2k
Avg merge
1d 21h
Merged PRs (30d)
92

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.

More from vitest-dev/vitest

All issues in vitest-dev/vitest

Similar issues

More TypeScript issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.