Replies: 12 comments
-
I'm not sure what you are suggesting. It's helpful if you have a concrete example to demonstrate how new API |
Beta Was this translation helpful? Give feedback.
-
Hello @JorensM. Please provide a minimal reproduction using a GitHub repository or StackBlitz (you can also use examples). Issues marked with |
Beta Was this translation helpful? Give feedback.
-
Sorry for the ambiguity. Here is an example:
If it's still not clear what the use case would be, please see https://time-to-sleep.vercel.app/breathing as an example of what I mean. Here is another example of this being done in FL Studio (music production software) to set the BPM of the track: https://youtu.be/pxgv6BA0r2c?si=SPTXCa0dYoQenPhn&t=260 (at 4:20) |
Beta Was this translation helpful? Give feedback.
-
@JorensM I appreciate the example, but I failed to see what's intended to happen in |
Beta Was this translation helpful? Give feedback.
-
@hi-ogawa It's not conceptually different, in both cases the program waits for the time to pass before proceeding, but I thought maybe we could come up with a more efficient and precise method that plays along well with the testing library. As noted before, currently implementing it in the way that you suggested is inconsistent and sometimes the program waits for far longer than the specified time. I'm guessing that this is due to some background processes happening with the library that slows down the execution of the test. |
Beta Was this translation helpful? Give feedback.
-
At the moment, I don't have any idea where that inconsistency comes from. Can you provide a reproduction? I guess that can be said a bug as well. Otherwise, we don't know what kind of "a more efficient and precise method" you're looking for. |
Beta Was this translation helpful? Give feedback.
-
Sure, you can clone https://github.com/JorensM/time-to-sleep and run the as for 'a more efficient and precise method', I wasn't talking about optimizing it, sorry if it sounded that way. I just suggested that something in vitest might be interfering with the |
Beta Was this translation helpful? Give feedback.
-
Looking at https://github.com/JorensM/time-to-sleep/blob/0964a548084a8b9e0cd4244b83e5e349848d0575/src/__tests__/BreathingExercises.test.tsx#L38-L44, it's possible that each |
Beta Was this translation helpful? Give feedback.
-
So the dates show a correct, 2 second difference. So the sleep function does indeed wait for 2 seconds. The recorded interval: This would suggest that either the element does not get found right away and auto-retries, or that Another possibility is that the initial |
Beta Was this translation helpful? Give feedback.
-
Could it also be possible that my CPU hangs which causes the delay? Or does JavaScript take that into account? |
Beta Was this translation helpful? Give feedback.
-
If you want to test timing precisely, you should not rely on real timers. Instead, use + vi.useFakeTimers();
await expect.element(initialText).toBeVisible();
await initialText.click();
await expect.element(breatheInText).toBeVisible();
- await sleep(2000);
+ vi.advanceTimersByTime(1999)
+ // In-text should still be visible
+ await expect.element(breatheInText).toBeVisible();
+ // Out-txt should not yet be visible
+ await expect.element(breatheOutText).toBeVisible();
+ vi.advanceTimersByTime(1)
+ // Should become visible now
await breatheInText.click();
await expect.element(breatheOutText).toBeVisible(); |
Beta Was this translation helpful? Give feedback.
-
I'll move this to a discussion as I'm not sure the exact objective of the feature and this feels more like a question of your app's testing strategy. Please try out Ari's fake timer approach. |
Beta Was this translation helpful? Give feedback.
-
Clear and concise description of the problem
I'd like to have a waitForTime() utlity function that would wait a specified amount of time before proceeding with the next lines of code. I know I know, manual waiting is frowned upon in testing, but I have a real case where I need to run a click/keyboard event at specific intervals of time, because such is simply the nature of the component.
Suggested solution
Honestly no idea.
Alternative
Right now I've implemented a function:
But it's inconsistent and sometimes the recorded intervals are much longer than the provided sleep value. I'd be okay with some margin of error, but right now it tends to wait for 60 seconds when I only specified to sleep for 2 seconds.
Additional context
Basically, the component I want to test is used to set a rhythm by repeatedly tapping/clicking on the page. So yeah, I'd like to test this feature if possible.
Validations
Beta Was this translation helpful? Give feedback.
All reactions