-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Open
Labels
Description
What you did:
A simple update from v13 to v14 broke my Vitest-based test where I was using await user.click(...)
as the promise no longer resolves.
Reproduction:
Run repo at the following commit: wojtekmaj/react-async-button@fa41b3b
Suggested solution:
After long debug session, I have determined that
- Monkey patching
asyncWrapper
to be justcb => cb()
resolves the issue. - Removing the following code added in fix: Prevent "missing act" warning for queued microtasks #1137 resolves the issue:
react-testing-library/src/pure.js
Lines 41 to 52 in f78839b
// Drain microtask queue. | |
// Otherwise we'll restore the previous act() environment, before we resolve the `waitFor` call. | |
// The caller would have no chance to wrap the in-flight Promises in `act()` | |
await new Promise(resolve => { | |
setTimeout(() => { | |
resolve() | |
}, 0) | |
if (jestFakeTimersAreEnabled()) { | |
jest.advanceTimersByTime(0) | |
} | |
}) |
- Moving
if (jestFakeTimersAreEnabled()) { ... }
to wrap the entire block mentioned above resolves the issue. - Calling
vi.advanceTimersByTime(0);
manually afteruser.click(...)
but beforeawait
ing returned promise, even multiple times, does NOT help - The only workaround that worked for me was this: wojtekmaj/react-async-button@2d26f21
So my suggestion is to:
- Roll back the fix and perhaps reintroduce when advanceTimers will be configurable and not jest dependent
- OR move
if (jestFakeTimersAreEnabled()) { ... }
to wrap the entire block mentioned above, acknowledging that the fix is now Jest-only.
wojtekmaj, ctjhoa, robinvw1, BreakBB, nickserv and 34 more
Metadata
Metadata
Assignees
Labels
Type
Projects
Milestone
Relationships
Development
Select code repository
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/react
Lokua commentedon Mar 28, 2023
I was about to submit what I think is the same issue.
await userEvent.click
causes any test that is ran after callingjest.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.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.click
ph-fritsche commentedon Mar 29, 2023
@Lokua You need to set the
advanceTimers
option 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
setTimeout
call inasyncWrapper
and is independent ofuser-event
. It just happens to use it.When we use
setTimeout
inuser-event
we also call theadvanceTimers
callback.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,
setup
should actually be calledrender
, or vis a versa, yes?ph-fritsche commentedon Mar 29, 2023
@Lokua Yes, the
setup
function 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
advanceTimers
option. 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
userEvent.click()
fails when used withvi.useFakeTimers()
, all available solutions are not working testing-library/user-event#1115userEvent.click()
fails when used withvi.useFakeTimers()
, all available solutions are not working vitest-dev/vitest#318436 remaining items