Repository navigation
Labels
p3-minor-bugAn edge case that only affects very specific usage (priority)
Description
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.0Used Package Manager
pnpm
Validations
- Follow our Code of Conduct
- Read the Contributing Guidelines.
- Read the docs.
- Check that there isn't already an issue that reports the same bug to avoid creating a duplicate.
- Check that this is a concrete bug. For Q&A open a GitHub Discussion or join our Discord Chat Server.
- The provided reproduction is a minimal reproducible example of the bug.
Reacted by rtowarek-krk
Metadata
Metadata
Assignees
Labels
p3-minor-bugAn edge case that only affects very specific usage (priority)
Type
Projects
- StatusShow more project fieldsApproved
Activity
hi-ogawa commentedon Apr 4, 2026
Can you explain why this is an unexpected behavior? It looks to me fake timer stalling
setTimeoutand thusshownever becomes true.radqut commentedon Apr 4, 2026
@hi-ogawa If we use a real timer, we don't need to check whether an element exists — we can just call
.clickdirectly. With a fake timer, however, we need to advance the timer and usevi.waitForto 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.sheremet-va commentedon Apr 4, 2026
You can call vi.setTimerTickMode(“nextTimerAsync”) to advance timers automatically.
hi-ogawa commentedon Apr 4, 2026
I'd also hope
nextTimerAsyncsaving all the issues and we should suggest more, but I think one confusion might beexpect.element, vi.waitFor/waitUntilauto advances, butclickor actions don't. Probably currently it would work like:I think we should still handle this from documentation perspective.
radqut commentedon Apr 4, 2026
It does not work the same. Here is an example:
[-]commands does not work with fake timers[/-][+]fake timer doesn't auto advance for browser mode actions[/+]fix(browser): advance fake timers during locator actions
fix(browser): advance fake timers during locator actions
fix(browser): advance fake timers during locator actions
fix(browser): advance fake timers during locator actions
fix(browser): advance fake timers during locator actions
hi-ogawa commentedon Oct 19, 2026
Just revisiting the
setTimerTickMode("nextTimerAsync")repro above (#10058 (comment)). It fails because the example component flipshidden -> shown -> hiddenthrough twosetTimeouts, which leaves the button visible for a 1000ms window.nextTimerAsyncruns 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 undernextTimerAsync, so it isn't specific to actions.Thinking about it more, this suggests
nextTimerAsyncisn'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 exampleawait vi.advanceTimersByTimeAsync(1000)before the click. But implementing auto advance at the action and assertion level, the wayexpect.elementalready does, also seems reasonable, which is what #11242 explores.