Extremely slow test performance with userEvent.type
- things to look for for a cause?
#977
-
I know there have been issues filed against this project regarding the performance of I have a pretty straightforward form component with controlled inputs. I have two tests that fill out the form - one using I mostly understand the difference in the two approaches - and that
This is a particularly large app and with some other forms we have issues with tests timing out or taking far too long because of this. In both cases, from my understanding, the component's inner workings are being exercised. The form has the correct values and the mock network request is made with the correct data. I don't see any obvious cause for the major difference in performance. So I have two questions... (1) Any particular patterns to look for that might explain what, in our code, is causing the performance issue with In the case of a form with text fields, what is the advantage of |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 2 replies
-
The two APIs might seem similar, but they are not. By using
Of course, just like there are scenarios where it is considered sufficient to not write any tests at all. If you have a complex project ("particularly large app"), I probably would tend to optimize the CI (e.g. not always running the full test suite) instead of trying to optimize tests (i.e. dispatching a concrete event instead of simulating the user workflow).
a. It might be that some code in the component under test is slow. E.g. some keyboard event handler that is triggered along the way. b. It might be that some API in the test environment (e.g. on the DOM implementation) is slow. E.g. we check for c. It might be that your performance measurement is misleading and the test itself isn't slow at all. If you just compare two timestamps this doesn't necessarily tell you the time spent executing this particular piece of code. |
Beta Was this translation helpful? Give feedback.
-
to improve testing speed try using userEvent.paste, instead of typing individual characters. Not sure how much of a speed improvement this will show. |
Beta Was this translation helpful? Give feedback.
The two APIs might seem similar, but they are not.
With
fireEvent.change
you dispatch achange
event. Simple and fast.With
userEvent.type
you simulate a user clicking on an element and pressing keys on the keyboard. A lot happens and this might lead to achange
event, but it probably doesn't.By using
user-event
you trade performance for accuracy. You remove the risk that assumptions in your test - e.g. that achange
event will be dispatched - might be wrong and something else might break your component. (See the blog post linked above on how easy it is to be wrong about such a basic assumption.)You remove the need…