userEvent.type - Only typing in the first letter of my provided input on React 17
See original GitHub issue@testing-library/user-event
version: 12.6.0
- Testing Framework and version: Jest 26.6.3
- DOM Environment: jsdom 16.4.0
What you did: I set up a test to check the input value after typing with text of “Input”
What happened: The input value in the test was only set to “I”
Reproduction repository: https://codesandbox.io/s/peaceful-surf-fedc7?file=/src/App.spec.js
Problem description: The userEvent is not firing off completely and is only typing the first letter of my provided input on React 17. This is leading to cases where I can’t write complete tests currently. I am not totally sure if this will help but there are similar issues with fireEvent as well.
Suggested solution:
Issue Analytics
- State:
- Created 3 years ago
- Comments:8 (3 by maintainers)
Top Results From Across the Web
React Testing Library userEvent.type recognizing only the first ...
As you see, it is executing the test without any useRouter errors but not recognizing entire text. It just takes the first letter...
Read more >Only typing in the first letter of my provided input on React 17
userEvent.type - Only typing in the first letter of my provided input on React 17. ... @testing-library/user-event version: 12.6.0.
Read more >user-event v13 - Testing Library
You should use userEvent.type if you just want to conveniently insert some text into an input field or textarea. Keystrokes can be described:....
Read more >Simulate Typing into Form Fields in Tests with the User Event ...
Now, type userevent.clearinput. We're going to clear out whatever is in there. We're going to clear out the input field. Now that we...
Read more >Common mistakes with React Testing Library - Kent C. Dodds
Despite our efforts to document the "better way" to use the utilities we provide, I still see blog posts and tests written following...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
This one is tricky.
Remember variables are bound to their declaration. Properties are resolved when the expression is executed.
Now React hooks are no language constructs and you can use them in any way, but I guess it doesn’t make life easier. While
setState
with a reducer works, I suggest you useuseReducer
for that.Regarding the issue, I think it should be closed as out of scope, but someone else should have a look at this first. While it does behave differently in the browser, the reasons lie deep down in how fireEvent creates the Event on an element controlled by React. I’m not sure there is an easy way to fix this.
@ph-fritsche
I had the same problem and your solution saved the day for me, thank you! 👍
Like you wrote, the value had to be resolved before putting it inside the prevState thingy, like this:
const { value } = event.target;