testing-library / user-event

🐕 Simulate user events
https://testing-library.com/user-event
MIT License
2.2k stars 249 forks source link

.click() not working when injectGlobals: false and fake timers enabled #1173

Open krutoo opened 1 year ago

krutoo commented 1 year ago

Reproduction example

https://github.com/krutoo/testing-library-user-event-with-fake-timers

Prerequisites

  1. clone repo
  2. npm install
  3. npm run test

Expected behavior

Tests should be done

Actual behavior

Test fails by timeout 5000ms

User-event version

14.5.1

Environment

Testing Library framework: @testing-library/react@12.1.3

JS framework: react@18.2.0

Test environment: jest@29.7.0

DOM implementation: jest-environment-jsdom@29.7.0

Additional context

In jest config I use injectGlobals: false and may be its the reason

I also use userEvent.setup({ advanceTimers: jest.advanceTimersByTime }) and its not solves issue.

krutoo commented 10 months ago

Can anyone confirm or deny?

korniux commented 10 months ago

Can reproduce

K3TH3R commented 9 months ago

I'm also seeing this happening with Vitest's vi.useFakeTimers(). I insert vi.useFakeTimers() globally as part of my setupFiles as I have a lot of date/time related tests, so the tests that are using userEvent.setup(), if I put vi.useRealTimers() at the start of the unit test, it allows them to run properly again.

K3TH3R commented 8 months ago

I'm also seeing this happening with Vitest's vi.useFakeTimers(). I insert vi.useFakeTimers() globally as part of my setupFiles as I have a lot of date/time related tests, so the tests that are using userEvent.setup(), if I put vi.useRealTimers() at the start of the unit test, it allows them to run properly again.

Update: I'm not doing vi.useFakeTimers() globally anymore and I'm still seeing this issue when I use vi.useFakerTimers() in a test that I use userEvent.setup() in.