-
Notifications
You must be signed in to change notification settings - Fork 264
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
Output is not helpful when ByText query fails #1375
Comments
Hi @stevehanson ! This looks like a very good idea! It is indeed quite difficult to see the state of the component when the query fails using findBy right now so I think this would be a good addition. Doing something similar to React Testing Library looks feasible, we could update the getMissingError from text queries and use format with mapProps option to log the elements without props. A simplified version would look like this const getMissingError = (text: TextMatch) =>
`Unable to find an element with text: ${String(text)}
${format(screen.toJSON() || [], { mapProps: () => ({}) })}`; This should be a good place to start and if other maintainers support this proposal I'd be more than happy if you opened a pr for it @MattAgn @mdjastrzebski @AugustinLF what do you think about this proposal? |
We've had some discussions around that. We agree that it's definitely something that should be improved. I know that @mdjastrzebski had a PR to |
You mean this pr? to have toJSON method not only on root? Yes it would allow to do some cool stuff. In the meantime we could print the root with only relevant props to the query so only children for text queries and children, accessibilityRole & accessibilityLabel & accessibilityState for role queries and so on |
Yep exactly, we could also consider using the default user's debug option if they set it up globally. |
Thank you for this excellent library! I'm a longtime user of RNTL, and something I have struggled with is that when a
ByText
query fails, the output does not help me determine the cause of the failure:When I encounter a failure like the above, my usual next step is to insert a
screen.debug()
or someconsole.log()
statements and rerun the test until I can understand the cause of the failure.If the test uses the async
findByText
query, then adding thesedebug
or log statements manually can be a bit more difficult, and I usually do something like this:Previous workaround (
toHaveTextContent
matcher)I recently found a workaround that fit my needs, using the jest-native
toHaveTextContent
matcher:As can be seen above, the
toHaveTextContent
matcher outputs all of the text from the screen along with the error message, which can be quite helpful for diagnosing why the test is failing.This workaround however no longer seems like a safe approach since in the latest version of RNTL
screen.container
is discouraged and has been renamed toUNSAFE_root
.Additionally, using the
toHaveTextContent
matcher is non-standard and something we have to train teams to use instead of theByText
queries. Ideally, we could solve this problem with theByText
queries themselvesComparing to React Testing Library (web)
As can be seen in this Code Sandbox, React Testing Library helpfully outputs the full DOM when a
byText
query fails:Proposal
A more helpful error message when a
byText
query fails would dramatically improve the testing experience with React Native. Ideally, we could achieve something like the error message that is shown on the web.I haven't contributed to this project before, but I'm a longtime user and would be happy to help out with a contribution if maintainers agree that this would be valuable.
The text was updated successfully, but these errors were encountered: