-
Notifications
You must be signed in to change notification settings - Fork 470
Sub-optimal error message when using text matcher functions #732
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
Comments
Did you try your |
Yeah, it works fine and is a super easy way for end users to improve the situation immediately. The only "solution" on the lib's part would be finessing the error message if a function is being used and possibly adding a brief note to the docs where it discusses matcher functions. |
I think a note in the docs would make sense. Would you be willing to add a PR for that? |
Sure. |
I've just ran into this same issue! The error message is very confusing, I was starting to think (reluctantly) that the library was doing a search using the source code as text. Could we have this adjusted for cases where a function is provided instead of a text string? |
I think it may look nicer to truncate the message to |
That makes sense, no need to show the source code if Jest does it already! Ideally it shouldn't mention text at all where a function matcher is provided, since it leads the debugging dev down the wrong path. |
Hi, |
When using a text matcher function with, e.g.
screen.getByText
, theTestingLibraryElementError
message is not accurate and possibly not helpful, in that it says that the source code of the function is the "text" (pattern) being matched against.I realize that this is low priority, because a) in some cases the matcher function will contain hard-coded comparison parameters, and b) if the comparison parameters happen to be hidden in outer-scope variables, the user can just look up the test case to see what the actual pattern or match parameters were.
The "not accurate" part would be relatively easy to fix in that the library could detect when the matcher is a function and use a more appropriate error message. (Aside: maybe it would be helpful if the current error message contained a link to a discussion of how to write a matcher function to match across elements).
The "possibly not helpful" part is true if the function doesn't contain literal comparison parameters (pattern, etc), so that the function source code doesn't reveal the pattern(s) used. The documentation could mention the possibility of using a custom
toString
method on such custom matcher functions.Versions:
@testing-library/dom
version: 7.21.4, indirectly via @testing-library/react 10.4.7Relevant code or config:
What you did:
What happened:
Suggested solution
I discussed this above. In the case of my example above, using a custom
toString
might look like this:The text was updated successfully, but these errors were encountered: