You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Update to v14 breaks @testing-library/user-event on Vitest #1197
I was about to submit what I think is the same issue. await userEvent.click causes any test that is ran after calling jest.useFakeTimers to fail in v14.4.3. Took me a pretty long time to isolate it, but it appears to be the combination of using fake timers and awaiting userEvent, as remove either of those things and the tests pass.
^ downgrading to v13 or using fireEvent, no issue.
Happy to create a separate issue if you feel this is not the same.
Edit: If this is the same issue, I suggest renaming it to better reflect the actual bug so others can find it more easily. Perhaps v14 causes tests using fake timers to fail when awaiting userEvent.click
This is an issue with the setTimeout call in asyncWrapper and is independent of user-event. It just happens to use it.
When we use setTimeout in user-event we also call the advanceTimers callback.
I think we should add the same option to @testing-library/react.
importDTLfrom'@testing-library/dom'constuserEventConfig={delay: 0,advanceTimers: jest.advanceTimersByTime,}constuserEventWait=()=>Promise.all([newPromise(r=>setTimeout(r,userEventConfig.delay)),userEventConfig.advanceTimers(userEventConfig.delay),])constsomeUserEventApi=()=>{returnDTL.getConfig().asyncWrapper(()=>{DTL.getConfig().eventWrapper(()=>document.dispatchEvent(newEvent('foo')))DTL.getConfig().eventWrapper(()=>document.dispatchEvent(newEvent('bar')))awaituserEventWait()DTL.getConfig().eventWrapper(()=>document.dispatchEvent(newEvent('baz')))awaituserEventWait()})}awaitsomeUserEventApi()// with non-jest fake timers this will time out because of https://github.com/testing-library/react-testing-library/blob/f78839bf4147a777a823e33a429bcf5de9562f9e/src/pure.js#L44-L52
Is there any ETA to fix this issue? It should be testing-framework agnostic instead of sticking with Jest
One temporarily workaround for Vitest users can be like this:
In your test suites using fake timers
import{beforeAll,vi,describe}from'vitest';describe('this suite uses fake timers',()=>{// Temporarily workaround for bug in @testing-library/react when use user-event with `vi.useFakeTimers()`beforeAll(()=>{const_jest=globalThis.jest;globalThis.jest={
...globalThis.jest,advanceTimersByTime: vi.advanceTimersByTime.bind(vi)};return()=>void(globalThis.jest=_jest);});})
Reacted by Connor Bär, Robin van Wijngaarden, Igor Danchenko, Alessio Marzo, samd-penn, K@mil, Àlex Marlés, Ajay Balachandran, Alex Tompkins, Guido Lange and 8 more
I don't think that's a real solution. What shouldAdvanceTime option does is advance fake timers with normal time, which kinds of defeat the purpose of using fake timers, and make your test flaky.
So far I didn't find any solution which would work for Vitest.
I don't think that's a real solution. What shouldAdvanceTime option does is advance fake timers with normal time, which kinds of defeat the purpose of using fake timers, and make your test flaky.
I agree it's not a real solution. But if you're dealing with dates in terms of days (my use case), weeks, months or years it's a perfectly fine workaround for now. If your use case needs the precision of milliseconds or seconds then it's not a good workaround.
I don't think that's a real solution. What shouldAdvanceTime option does is advance fake timers with normal time, which kinds of defeat the purpose of using fake timers, and make your test flaky.
I agree it's not a real solution. But if you're dealing with dates in terms of days (my use case), weeks, months or years it's a perfectly fine workaround for now. If your use case needs the precision of milliseconds or seconds then it's not a good workaround.
Fair enough - in some use cases it could be a valid solution.
Just wasted an hour or so fighting with this and had to swap to using fireEvent for the clicks in question, which I understand is discouraged. Would be great if this actually gets resolved.
I'd like to support more timers but so far no community contributions have been made to do that. And since I'm not using Vitest in any projects I'm involved in, I didn't have a use-case for myself. PRs are welcome though.
@eps1lon could we discuss & agree on what the API for configuring this should look like, so the community could help with implementation? I did file a PR (#1402) with an initial idea back in June, but haven't received any response or feedback.
Seems quite trivial to implement, I think mainly the question is whether it should be a single config function (advanceTimers), or with a separate check function (areFakeTimersEnabled or sth like that).
Would love to get this sorted and willing to put in the work.
Activity
Add workaround for https://github.com/testing-library/react-testing-l…
eps1lon commentedon Mar 25, 2023
Moving to user-event until we have a repro that's just using
@testing-library/reactLokua commentedon Mar 28, 2023
I was about to submit what I think is the same issue.
await userEvent.clickcauses any test that is ran after callingjest.useFakeTimersto fail in v14.4.3. Took me a pretty long time to isolate it, but it appears to be the combination of using fake timers and awaiting userEvent, as remove either of those things and the tests pass.Here is a repro using @testing-library/react:
https://github.com/Lokua/user-event-repro
^ downgrading to v13 or using fireEvent, no issue.
Happy to create a separate issue if you feel this is not the same.
Edit: If this is the same issue, I suggest renaming it to better reflect the actual bug so others can find it more easily. Perhaps
v14 causes tests using fake timers to fail when awaiting userEvent.clickph-fritsche commentedon Mar 29, 2023
@Lokua You need to set the
advanceTimersoption if you're working with fake timers.We recommend using a setup function:
ph-fritsche commentedon Mar 29, 2023
@wojtekmaj @eps1lon
This is an issue with the
setTimeoutcall inasyncWrapperand is independent ofuser-event. It just happens to use it.When we use
setTimeoutinuser-eventwe also call theadvanceTimerscallback.I think we should add the same option to
@testing-library/react.Lokua commentedon Mar 29, 2023
Thank you very much for the info. I get it now. Just for clarity, in your example,
setupshould actually be calledrender, or vis a versa, yes?ph-fritsche commentedon Mar 29, 2023
@Lokua Yes, the
setupfunction should be called in the test, I fixed the code example above.runofthemillgeek commentedon Mar 31, 2023
@ph-fritsche I ran into the same issue but I couldn't get my scenario to work just with
advanceTimersoption. See repro link: https://stackblitz.com/edit/vitest-dev-vitest-2xdjiw?file=test%2Fcountdown.test.tsxIf I do:
It makes the test pass but I believe it's not a good option to use.
azat-io commentedon Apr 6, 2023
Same problem
bfsgr commentedon Apr 11, 2023
I believe this might be related to #1187 as vitest uses sinon fake timers
xsjcTony commentedon Apr 13, 2023
Is there any ETA to fix this issue? It should be testing-framework agnostic instead of sticking with
JestOne temporarily workaround for
Vitestusers can be like this:In your test suites using fake timers
13 remaining items
senock-dag commentedon Sep 29, 2023
Unfortunately the above solution didn't work for me. I've downgraded to
v13.5.0which has worked.Is a fix being worked on for future releases?
xuhdev commentedon Feb 15, 2024
@IanVS 's method works for me. Should we add this to the doc?
danielholmes commentedon Apr 25, 2024
The following worked for my case:
robertjk commentedon Apr 25, 2024
I don't think that's a real solution. What
shouldAdvanceTimeoption does is advance fake timers with normal time, which kinds of defeat the purpose of using fake timers, and make your test flaky.So far I didn't find any solution which would work for Vitest.
danielholmes commentedon Apr 25, 2024
I agree it's not a real solution. But if you're dealing with dates in terms of days (my use case), weeks, months or years it's a perfectly fine workaround for now. If your use case needs the precision of milliseconds or seconds then it's not a good workaround.
robertjk commentedon Apr 27, 2024
Fair enough - in some use cases it could be a valid solution.
✅ [open-formulieren/open-forms#4929] Add test for summary -> status -…
✅ [open-formulieren/open-forms#4929] Add test for summary -> status -…
✅ [open-formulieren/open-forms#4929] Add test for summary -> status -…
yaroslavvertukhov commentedon Jan 28, 2025
Any news on solving this problem?
yaroslavvertukhov commentedon Jan 28, 2025
So far my problem has been solved with a crutch
import { vi } from 'vitest'globalThis.jest = vikrutoo commentedon Feb 4, 2025
when will this problem be solved in this
testing-library/react?ardunster commentedon Jun 26, 2025
Just wasted an hour or so fighting with this and had to swap to using
fireEventfor the clicks in question, which I understand is discouraged. Would be great if this actually gets resolved.mihkeleidast commentedon Oct 22, 2025
@eps1lon could we discuss & agree on what the API for configuring this should look like, so the community could help with implementation? I did file a PR (#1402) with an initial idea back in June, but haven't received any response or feedback.
Seems quite trivial to implement, I think mainly the question is whether it should be a single config function (
advanceTimers), or with a separate check function (areFakeTimersEnabledor sth like that).Would love to get this sorted and willing to put in the work.
fix: add Vitest fake timer detection to asyncWrapper