-
Notifications
You must be signed in to change notification settings - Fork 150
New rule suggestion: no-test-id-queries #279
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
Hi @maryfsc, thanks for opening this issue! This could be an interesting idea to report usages of The example you shared is really direct, but what would you suggest if you have something like this? const item = screen.getByTestId('user-panel-data') I'd go just for a |
Hey @Belco90 I think that's a pretty good idea 😄 👍 |
Nice. Would you like to give this a try? I recommend to do this on v4 since new rules implementation is much easier there (and it should be released soon). |
Yes, I'd like to try! So I'll wait for v4 to release 😉 Thank you! |
That's awesome! We had other people implementing new rules directly on |
Hi again! Since v4 is finished, I hope I can spend time on this soon. It's a really interesting rule for encouraging people to use semantic and accessible queries rather than querying by id. |
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions. |
I still want to implement this one! |
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions. |
Note that, as of now, |
@zaicevas that's a good point, definitely something to mention in this rule's doc when implemented. I think the plugin should at least provide some rule to prevent or mitigate the usage of |
Upvote on the simple rule that either warns or errors if a test id query is used. I'm happy to collab on this, and maybe open a PR if it's simple enough. |
It would be great if you could open a PR! It should be fairly easy by using some of the internal helpers, you can find more indo in the contributing doc. |
Meanwhile this can be achieved thx to // .eslintrc.js
const noDataTestIdMessage =
'Tests should resemble how the user interacts with the application and should not rely on technical details, see https://testing-library.com/docs/queries/about/#priority';
...
reportUnusedDisableDirectives: true,
rules: {
'no-restricted-properties': [
'error',
// FIXME https://github.com/testing-library/eslint-plugin-testing-library/issues/279
{ object: 'screen', property: 'getByTestId', message: noDataTestIdMessage },
{ object: 'screen', property: 'queryByTestId', message: noDataTestIdMessage },
{ object: 'screen', property: 'getAllByTestId', message: noDataTestIdMessage },
{ object: 'screen', property: 'queryAllByTestId', message: noDataTestIdMessage },
{ object: 'screen', property: 'findByTestId', message: noDataTestIdMessage },
{ object: 'screen', property: 'findAllByTestId', message: noDataTestIdMessage }
],
'react/forbid-dom-props': [
'error',
{
// FIXME https://github.com/testing-library/eslint-plugin-testing-library/issues/279
forbid: [{ propName: 'data-testid', message: noDataTestIdMessage }]
}
], Always test your code: // no-restricted-properties.test.tsx
import { render, screen } from '@testing-library/react';
// FIXME https://github.com/testing-library/eslint-plugin-testing-library/issues/279
test('ban *ByTestId', async () => {
// eslint-disable-next-line react/forbid-dom-props
render(<div data-testid="foobar" />);
// eslint-disable-next-line no-restricted-properties
screen.getByTestId('foobar');
// eslint-disable-next-line no-restricted-properties
screen.queryByTestId('foobar');
// eslint-disable-next-line no-restricted-properties
screen.getAllByTestId('foobar');
// eslint-disable-next-line no-restricted-properties
screen.queryAllByTestId('foobar');
// eslint-disable-next-line no-restricted-properties
await screen.findByTestId('foobar');
// eslint-disable-next-line no-restricted-properties
await screen.findAllByTestId('foobar');
}); I suggest to enable |
Hi! I've been wondering if there was ever a discussion about implementing a rule to warn about accessible queries use. Since I don't think it's really necessary to throw an error or do an autofix, a warning should be just fine. So we could get a warning to possibly refactor this
into this
What do you think? :)
The text was updated successfully, but these errors were encountered: