question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Bug - Inconsistent behavior between browser and test environment with react-hook-form with react-select

See original GitHub issue
  • @testing-library/react version: 11.2.7
  • Testing Framework and version: Jest + bundled version with CRA version 4.0.3
  • DOM Environment: bundled version with CRA version 4.0.3

Relevant code or config:

react-hook-form-jest-issues.zip

I have tried to reproduce this issue in code sandbox, but due to some issue with code sandbox, I am unable to click a react-select element in a test. I have opened an issue with the code sandbox client for this. Therefore I have attached a simple reproduction using create-react-app.

https://github.com/codesandbox/codesandbox-client/issues/6257

What you did:

Ran a test that selects an option from a react-select component.

What happened:

The form management library (react-hook-form in this case) did not update its internal state correctly inside of the testing environment only. Specifically, the library is reporting that errors are present in the formState.errors object when there should be no errors. If the actions in the test are replicated on the browser, no errors are present after the option is selected which is the expected behavior.

Reproduction:

  1. Open the attached project
  2. Run npm install
  3. Run npm start and open the project in your browser
  4. Select an option from the select input. Open the console. Notice that the final console log of formState.errors is an empty object and formState.isValid is true as expected .
  5. Run npm test and select all tests using the a key
  6. Notice that the final console log of the test reports formState.errors as not being empty. Instead, an error is present while formState.isValid is true. This is incorrect and does not match how the browser logs.

The output from jest is: formState.errors: [object Object] formState.isValid: true The output from the browser is: formState.errors: undefined formState.isValid: true

Problem description:

This is a problem since I cannot write accurate tests for a large portion of my app. The issue seems to be isolated to react-hook-form version 7.15.1 and above, react-select, and inputs that use the field array feature inside react-hook-form. I spoke to the react-hook-form author and he does not believe this is an issue with the react-hook-form library as the correct behavior occurs in the browser.

Suggested solution:

I’m not sure what the correct code solution is, but it seems to be related to context not being updated correctly in certain cases within the testing environment.

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:6 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
wdfinchcommented, Dec 2, 2021

It seems this has been resolved in newer react-hook-form versions.

0reactions
ph-fritschecommented, Dec 6, 2021

Thanks for the update on this.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Returning correct value using react-select and react-hook-form
Everything works fine except the returned value is coming back as the string "[object Object]" instead of the fullName property from the object....
Read more >
Suspense for Data Fetching (Experimental) - React
To compare these approaches, we'll implement a profile page with each of them. Approach 1: Fetch-on-Render (not using Suspense). A common way to...
Read more >
React Hooks - Understanding Component Re-renders - Medium
This is a long post covering most of the built-in hooks with lot of screenshots and examples. If you are interested in only...
Read more >
The React Handbook – Learn React for Beginners
The React Handbook follows the 80/20 rule: learn in 20% of the time the 80% of a topic. I find this approach gives...
Read more >
Changelog - Cypress Documentation
Users working in React Component Testing projects can now generate a basic spec ... browser in Cypress 10.8.0 where the CYPRESS_EVERY_NTH_FRAME environment ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found