-
Notifications
You must be signed in to change notification settings - Fork 470
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
feat: Improve performance of findBy*/findAllBy* by not generating informative error messages #1071
Open
timjb
wants to merge
2
commits into
testing-library:main
Choose a base branch
from
timjb:pr/improve-findBy-performance
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I like the idea of using
then
here, but I think we could make this change easier to implement?Because
suggest
is an option, I was thinking we could use this parameter, and only enable it on the last try (in thethen
block).Also, do you know how much of an impact this has for the execution time/resources?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hi @timdeschryver, thanks for your feedback!
I don't think that using
suggestions
makes sense, because suggestions behave completely opposite to query error messages:throwSuggestions
enabled).Following your proposal would mean turning suggestions always off except on that last try for the
findBy*
/findAllBy*
queryies, completely ignoring thethrowSuggestions
setting.We could of course introduce a new field
elaborateErrorMessage
toMatcherOptions
(first set to false, then to true likesuggest
) and use that instead to decide how much effort should be put into producing a nice error message. One thing that I do not like about that is that I cannot imagine a user which would want to set this option to false. As a user I would always want the error messages that I get to see to be as detailed and informative as possible and I would also always want the library to not put much effort into producing error messages that I won't get to see. Therefore putting this option into the public API seems wrong to me.Regarding the impact on execution time/resources: I am not sure how to properly measure that. The impact will also depend strongly on the size of the DOM that is rendered in the test (the larger the DOM the larger the impact). I believe that I could come up with an example where the DOM has a similar size to the case that I've seen at work and this change speeds up the test by 1-5 seconds.
I want to add that there is one additional benefit to this change: It also improves reliability of test cases which render a huge DOM. Suppose that a
findBy*
query doesn't return a result on the first attempt. Without this change the library would produce an error message. Because of the size of the DOM this will take a considerable amount of time. If this amount of time is longer than thetimeout
milliseconds, then no second attempt will be made. Therefore the test will succeed or fail depending on whether the first attempt is successful or not. This can easily result in a blinker test. This change fixes this problem by making sure that producing the error message is cheap (untiltimeout
milliseconds have elapsed).There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah, I see thanks for the elaboration @timjb.
You're right, I was looking at the wrong path.
I would assume that if we go for the "fix" by using
then
to execute the query for a last time (and with diagnostics) that the option_disableExpensiveErrorDiagnostics
can be deprecated and removed in a future version because it won't be as process-heavy as before. This, regardless of the implementation details (using two queries as proposed in this PR, or by adding a new option as with the suggestion, or something else ...). Does this match with your thoughts?I'm not very familiar with these internals of DTL, but this seems like a reasonable enhancement and a better fix than the original
_disableExpensiveErrorDiagnostics
because the user gets a faster/better message.What do you think @eps1lon?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In my opinion,
_disableExpensiveErrorDiagnostics
shouldn't be removed even after this PR is merged because doing so would negatively impact the performance of the following code:In the top comment in this PR I have described an alternative approach that builds upon
_disableExpensiveErrorDiagnostics
rather than abolishing it. This approach would not just improve the performance of code usingfindBy*
/findAllBy*
, but of all code that uses element queries insidewaitFor
, which is why I am in favor of that alternative approach. The reason I didn't implement it right away is that it only occurred to me after I implemented the current version and I wanted to get some feedback first before rewriting it.