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: suggestions for which query to use #586
feat: suggestions for which query to use #586
Conversation
This pull request is automatically built and testable in CodeSandbox. To see build info of the built libraries, click here or the icon next to each commit SHA. Latest deployment of this branch, based on commit 9cec773:
|
Codecov Report
@@ Coverage Diff @@
## master #586 +/- ##
=========================================
Coverage 100.00% 100.00%
=========================================
Files 23 24 +1
Lines 468 535 +67
Branches 115 133 +18
=========================================
+ Hits 468 535 +67
Continue to review full report at Codecov.
|
src/query-helpers.js
Outdated
suggestions.length === 1 && | ||
!allQuery.name.endsWith(rawSuggestions[0].queryName) | ||
) { | ||
throw getSuggestionError(suggestions[0], container) |
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.
@kentcdodds was pairing on this with my friend Kevin and he asked a good question. Should the suggestion Try to actually execute the query it's suggesting just to make sure it works? Curious to hear your thoughts.
..
|
||
function queryAllByPlaceholderText(...args) { | ||
return queryAllByAttribute('placeholder', ...args) | ||
} |
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.
ICYW: all of the other queries use named functions and the suggestion feature needs them to be named.
queryAllByAttribute(getTestIdAttribute(), ...args) | ||
function queryAllByTestId(...args) { | ||
return queryAllByAttribute(getTestIdAttribute(), ...args) | ||
} |
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.
ICYW: all of the other queries use named functions and the suggestion feature needs them to be named.
Copy from Twitter again 😇 @benmonro How do you feel about using an "instance" level for it instead? screen.showSuggestions(false);
document.querySelector('input'); // don't throw this time
screen.showSuggestions(true); Or an alternative, a wrapping method like Name it const { getByText } = strict(document.getElementById('messages'));
const helloMessage = getByText('hello') This could then be accompanied by a |
@kentcdodds @eps1lon @smeijer I tried using warn (screenshot below). I'm definitely not a fan for 2 reasons:
|
Do I understand correctly that you only throw for Heck, I can imagine something like that being default in a future version. |
no, I'm not currently having this warn on usages of querySelector, that would be a bit sketchy imo as we'd have to change the way querySelector works. Also I feel like that one can be done w/ a lint rule. As for getByTestId, no I do not only throw on that. It throws whenever a better option is available. If we use warn we'd have to parse the stack trace (not sure when I'd have cycles to do that) have a look at my screenshot above (or feel free to pull my branch and play with it). If you can get the stack trace parsing working I'd be fine w/ having it use a warn (or maybe make that optional as well?). We can do I'll lay out the options here (in no particular order) for how to disable the suggestion for a single query, feel free to add more options, but maybe we can take a vote? option 1: an additional query option in the query itself: screen.getByTestId("foo", {suggest: false}) option 2: just use configure: configure({showSuggestions:false});
screen.getByTestId("foo")
configure({showSuggestions:true}); option 3: Stephan's screen.showSuggestions(false);
document.querySelector('input'); // don't throw this time
screen.showSuggestions(true); option 4: more granular configuration option for showSuggestions configure({
showSuggestions: { ByLabelText: "warn", ByTestId: "error" }
}) |
Option 5 // strict || strictA11y || ...
const { getByText } = strict(document.getElementById('messages'));
const helloMessage = getByText('hello') A method like that fits nicely together with Talking about I do agree, that is a must for this to work nicely. Showing incorrect code frames is nothing more than confusing. |
@smeijer ah ok i'll look into how that prints the stack trace. w/ regards to using |
edit Figured it out, and deleted some background info, you can still see it under the "edits". What I'm proposing is an API like this: import { screen, within } from '@testing-library/dom';
const view = within(document.getElementById('messages'), { strict: true });
view.getByText('hello'); // might throw for a11y issues
screen.getByText('hello'); // still not throwing This would play nicely together with your import { screen, getByText } from '@testing-library/dom';
screen.getByText('hello', { strict: true }); // might throw
getByText(document, 'hello', { strict: true }); // might throw ps. I'm deliberately naming it |
@kentcdodds @smeijer all the feedback has been addressed. docs PR is good to merge as well as soon as this merges. |
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.
Really looking forward to this 👍
Should the extra config suggest
also be added to the matcher types?
Yes 👍 |
@timdeschryver @kentcdodds added the type to matcher types. |
…-testing-library into feature/suggestions
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.
Stellar! Thanks!
🎉 This PR is included in version 7.7.0 🎉 The release is available on:
Your semantic-release bot 📦🚀 |
@allcontributors please add @appleJax for code & tests |
I've put up a pull request to add @appleJax! 🎉 |
@allcontributors please add @benmonro for code, ideas, tests, docs |
I've put up a pull request to add @benmonro! 🎉 |
🎉 Nice work @benmonro ! Thanks. |
I don't think that this pull-request would have been here if it wasn't for http://testing-playground.com, so how about this one as well? @allcontributors please add @smeijer for ideas |
I've put up a pull request to add @smeijer! 🎉 |
@smeijer yep yep |
What:
Adding suggestions for which query to use. (opt in via configure). When using
configure({throwSuggestions: true})
tests will fail if they are not using the suggested queries. While this may seem harsh, the intent is more along the lines of the way that--detectLeaks
works in jest.Why:
To encourage people to follow best practices when using TL.
How:
a new getSuggestedQuery function that follows the order of 'which query should i use?'
Started going down the road of having a separate library but it became apparent pretty quick that it made more sense for this to live in DTL directly. We can export the suggestion from here for things like testing-playground.com
Docs PR: https://github.com/testing-library/testing-library-docs/pull/477/files
Checklist:
docs site
DefinitelyTyped