Skip to content

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

Open
maryfsc opened this issue Feb 10, 2021 · 15 comments · May be fixed by #1006
Open

New rule suggestion: no-test-id-queries #279

maryfsc opened this issue Feb 10, 2021 · 15 comments · May be fixed by #1006
Labels
new rule New rule to be included in the plugin pinned Pinned for different reasons. Issues with this label won't be flagged as stale by stalebot

Comments

@maryfsc
Copy link

maryfsc commented Feb 10, 2021

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

const loginButton = screen.getByTestId('login-button');

into this

const loginButton = screen.getByRole('button', { name: /login/i });

What do you think? :)

@Belco90 Belco90 added the new rule New rule to be included in the plugin label Feb 10, 2021
@Belco90
Copy link
Member

Belco90 commented Feb 10, 2021

Hi @maryfsc, thanks for opening this issue!

This could be an interesting idea to report usages of ByTestId queries. However, I'm not really sure about how to suggest from ByTestId argument which other query to use instead + what pass to it.

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 no-test-id-queries rule which complains if you use a ByTestId query, without suggesting how to fix it and just saying something like "Using data-testid attributes do not resemble how your software is used and should be avoided if possible".

@maryfsc
Copy link
Author

maryfsc commented Feb 11, 2021

Hey @Belco90 I think that's a pretty good idea 😄 👍

@Belco90
Copy link
Member

Belco90 commented Feb 12, 2021

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).

@maryfsc
Copy link
Author

maryfsc commented Feb 15, 2021

Yes, I'd like to try! So I'll wait for v4 to release 😉 Thank you!

@Belco90
Copy link
Member

Belco90 commented Feb 15, 2021

That's awesome! We had other people implementing new rules directly on v4 branch, so you could do that if you want to. However, I have to tell you that the doc around new rules creator is not added yet, guidelines weren't updated properly, and new internal mechanisms introduced in v4 could still change (although it's pretty stable already). So up to you!

@Belco90 Belco90 changed the title New rule suggestion: prefer-accessible-query New rule suggestion: no-test-id-queries Mar 29, 2021
@Belco90 Belco90 self-assigned this Apr 20, 2021
@Belco90
Copy link
Member

Belco90 commented Apr 20, 2021

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.

@stale
Copy link

stale bot commented Jun 19, 2021

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.

@stale stale bot added the wontfix This will not be worked on label Jun 19, 2021
@Belco90
Copy link
Member

Belco90 commented Jun 20, 2021

I still want to implement this one!

@stale stale bot removed the wontfix This will not be worked on label Jun 20, 2021
@stale
Copy link

stale bot commented Aug 19, 2021

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.

@stale stale bot added the wontfix This will not be worked on label Aug 19, 2021
@Belco90 Belco90 added pinned Pinned for different reasons. Issues with this label won't be flagged as stale by stalebot and removed wontfix This will not be worked on labels Aug 20, 2021
@zaicevas
Copy link
Contributor

Note that, as of now, role queries are incredibly slow for larger DOM trees (see testing-library/dom-testing-library#552). Test ids is one of the ways to mitigate this. In my experience, certain tests are almost 1 second (!) faster with test ids, as opposed to with role queries.

@Belco90
Copy link
Member

Belco90 commented Nov 24, 2021

@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 ByTestId queries, then it's up to each dev/team to enable it or not.

@arinthros
Copy link

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.

@Belco90
Copy link
Member

Belco90 commented Apr 11, 2022

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.

@Belco90 Belco90 removed their assignment Oct 17, 2022
@tkrotoff
Copy link

Meanwhile this can be achieved thx to no-restricted-properties and react/forbid-dom-props:

// .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 reportUnusedDisableDirectives: https://eslint.org/docs/latest/use/configure/rules#report-unused-eslint-disable-comments (should be ESLint default)

StyleShit added a commit to StyleShit/eslint-plugin-testing-library that referenced this issue May 11, 2025
@StyleShit StyleShit linked a pull request May 11, 2025 that will close this issue
@Belco90 Belco90 closed this as completed May 12, 2025
@Belco90 Belco90 reopened this May 12, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
new rule New rule to be included in the plugin pinned Pinned for different reasons. Issues with this label won't be flagged as stale by stalebot
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants