-
Notifications
You must be signed in to change notification settings - Fork 470
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
waitFor with fake timers does not work with getByRole
#1108
Comments
@dannyharding10 When you use fake timers in Jest, you're removing the dependence on real time for the test, to allow your test environment to control the passage of time. https://jestjs.io/docs/timer-mocks#run-all-timers This should work for you: test('waitFor should work when used with fake timers and a long WAIT_TIME', async () => {
jest.useFakeTimers()
const WAIT_TIME = 80000
const MESSAGE = 'Displayed Message'
render(<Test delay={WAIT_TIME} message={MESSAGE} />)
act(() => {
jest.runAllTimers();
});
await waitFor(() => {
expect(screen.getByText(MESSAGE)).toBeInTheDocument()
});
expect(screen.getByRole('heading', { name: MESSAGE })).toBeInTheDocument()
jest.useRealTimers()
}) I'd also avoid putting byRole queries in a waitFor loop, as they are very computationally expensive - big thread about that here: #820. Eslint would also throw a warning about your multiple assertions in a waitFor callback too, the following explains why: |
I'm also experiencing fake timers not working with |
Any update on this? I ran into another similar issue where waitForElementToBeRemoved does not seem to be advancing the fake time as it should according to #662 |
@dannyharding10 Could you create a minimal, cloneable repository that reproduces the problem? This would help a lot debugging this issue. |
@eps1lon Here's a repro using the code from above. https://github.com/dannyharding10/waitFor-useFakeTimers-fail Let me know if anything else is needed! |
@eps1lon do you need anything else to investigate this further? I see similar things when using |
The behavior in the repro is expected. The default interval for It's hard to give the correct advise here but fine tuning |
@eps1lon Thanks for looking into this! I'm assuming that you are getting the number 0.315ms from 5000 / 16000? If I understand correctly, you are saying that when we use fake timers, testing-library still uses the default interval of 50 ms, but that time is advanced virtually. So then the actual limit being imposed is not in So is it just not advised to use |
All correct 👍🏻
No that's perfectly fine. It just works with a consistent clock. Whether that is fake or real, the number of invocations of the callback are equal in real and fake clocks. |
@testing-library/dom
version:7.22.6 (via
@testing-library/react
v10.4.9)jest v26.6.0 (via
react-scripts
v4.0.3)jsdom 16.6.0 (via
react-scripts
v4.0.3)Relevant code or config:
What you did:
Trying to test a component that depends on an interval to fetch some data. The code above is a much more simplified version with no network, no promises, etc.
What happened:
When trying to query the screen with
getByRole
, the test would fail. UsinggetByText
allowed it to succeed. This only happens when the wait time is significantly larger than the defaultwaitFor
timeout.Reproduction:
The code above is all that is needed to break it. Here's a minimal repro https://github.com/dannyharding10/waitFor-useFakeTimers-fail
Problem description:
This was extremely confusing to come across, especially after I saw #661 and the corresponding PR that fixed it (at least mostly 😅 ). I expected
waitFor
to either always work or always fail in this situation, but it seems to work only sometimes.Suggested solution:
I don't think it makes sense to allow
waitFor
to just go forever, but it would be nice if the behavior were more consistent. If the asynchronous action took longer than the timeout forwaitFor
, then it should fail 100% of the time. Right now, it seems like it succeeds and fails conditionally depending on the complexity of the query (*ByRole
vs*ByText
), the duration (always fails with a crazy high delay, like 1_000_000+), etc.The text was updated successfully, but these errors were encountered: