-
Notifications
You must be signed in to change notification settings - Fork 150
feat: add no-test-id-queries
rule
#1006
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
Merged
Belco90
merged 6 commits into
testing-library:main
from
StyleShit:feat/279-no-test-id-queries
May 13, 2025
+167
−1
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
fb59333
feat: add `no-test-id-queries` rule
StyleShit 45c70c2
test: fix tests
StyleShit dc23733
fix: cr changes
StyleShit 50c44c6
docs: generate
StyleShit 4090c56
fix: remove from recommended config
StyleShit 6ce243a
docs: remove from recommended configs
StyleShit 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 hidden or 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 hidden or 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
# Ensure no `data-testid` queries are used (`testing-library/no-test-id-queries`) | ||
|
||
<!-- end auto-generated rule header --> | ||
|
||
## Rule Details | ||
|
||
This rule aims to reduce the usage of `*ByTestId` queries in your tests. | ||
|
||
When using `*ByTestId` queries, you are coupling your tests to the implementation details of your components, and not to how they behave and being used. | ||
|
||
Prefer using queries that are more related to the user experience, like `getByRole`, `getByLabelText`, etc. | ||
|
||
Example of **incorrect** code for this rule: | ||
|
||
```js | ||
const button = queryByTestId('my-button'); | ||
const input = screen.queryByTestId('my-input'); | ||
``` | ||
|
||
Examples of **correct** code for this rule: | ||
|
||
```js | ||
const button = screen.getByRole('button'); | ||
const input = screen.getByRole('textbox'); | ||
``` | ||
|
||
## Further Reading | ||
|
||
- [about `getByTestId`](https://testing-library.com/docs/queries/bytestid) | ||
- [about `getByRole`](https://testing-library.com/docs/queries/byrole) | ||
- [about `getByLabelText`](https://testing-library.com/docs/queries/bylabeltext) | ||
- [Common mistakes with React Testing Library - Not querying by text](https://kentcdodds.com/blog/common-mistakes-with-react-testing-library#not-querying-by-text) |
This file contains hidden or 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
import { TSESTree } from '@typescript-eslint/utils'; | ||
|
||
import { createTestingLibraryRule } from '../create-testing-library-rule'; | ||
import { ALL_QUERIES_VARIANTS } from '../utils'; | ||
|
||
export const RULE_NAME = 'no-test-id-queries'; | ||
export type MessageIds = 'noTestIdQueries'; | ||
type Options = []; | ||
|
||
const QUERIES_REGEX = `/^(${ALL_QUERIES_VARIANTS.join('|')})TestId$/`; | ||
|
||
export default createTestingLibraryRule<Options, MessageIds>({ | ||
name: RULE_NAME, | ||
meta: { | ||
type: 'problem', | ||
docs: { | ||
description: 'Ensure no `data-testid` queries are used', | ||
recommendedConfig: { | ||
dom: false, | ||
angular: false, | ||
react: false, | ||
vue: false, | ||
svelte: false, | ||
marko: false, | ||
}, | ||
}, | ||
messages: { | ||
noTestIdQueries: | ||
'Using `data-testid` queries is not recommended. Use a more descriptive query instead.', | ||
}, | ||
schema: [], | ||
}, | ||
defaultOptions: [], | ||
|
||
create(context) { | ||
return { | ||
[`CallExpression[callee.property.name=${QUERIES_REGEX}], CallExpression[callee.name=${QUERIES_REGEX}]`]( | ||
node: TSESTree.CallExpression | ||
) { | ||
context.report({ | ||
node, | ||
messageId: 'noTestIdQueries', | ||
}); | ||
}, | ||
}; | ||
}, | ||
}); |
This file contains hidden or 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 hidden or 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
import rule, { RULE_NAME } from '../../../lib/rules/no-test-id-queries'; | ||
import { createRuleTester } from '../test-utils'; | ||
|
||
const ruleTester = createRuleTester(); | ||
|
||
const SUPPORTED_TESTING_FRAMEWORKS = [ | ||
'@testing-library/dom', | ||
'@testing-library/angular', | ||
'@testing-library/react', | ||
'@testing-library/vue', | ||
'@marko/testing-library', | ||
]; | ||
|
||
const QUERIES = [ | ||
'getByTestId', | ||
'queryByTestId', | ||
'getAllByTestId', | ||
'queryAllByTestId', | ||
'findByTestId', | ||
'findAllByTestId', | ||
]; | ||
|
||
ruleTester.run(RULE_NAME, rule, { | ||
valid: [ | ||
Belco90 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
` | ||
import { render } from '@testing-library/react'; | ||
test('test', async () => { | ||
const { getByRole } = render(<MyComponent />); | ||
expect(getByRole('button')).toBeInTheDocument(); | ||
}); | ||
`, | ||
|
||
` | ||
import { render } from '@testing-library/react'; | ||
test('test', async () => { | ||
render(<MyComponent />); | ||
expect(getTestId('button')).toBeInTheDocument(); | ||
}); | ||
`, | ||
], | ||
|
||
invalid: SUPPORTED_TESTING_FRAMEWORKS.flatMap((framework) => | ||
QUERIES.flatMap((query) => [ | ||
{ | ||
code: ` | ||
import { render } from '${framework}'; | ||
test('test', async () => { | ||
const { ${query} } = render(<MyComponent />); | ||
expect(${query}('my-test-id')).toBeInTheDocument(); | ||
}); | ||
`, | ||
errors: [ | ||
{ | ||
messageId: 'noTestIdQueries', | ||
line: 7, | ||
column: 14, | ||
}, | ||
], | ||
}, | ||
{ | ||
code: ` | ||
import { render, screen } from '${framework}'; | ||
test('test', async () => { | ||
render(<MyComponent />); | ||
expect(screen.${query}('my-test-id')).toBeInTheDocument(); | ||
}); | ||
`, | ||
errors: [ | ||
{ | ||
messageId: 'noTestIdQueries', | ||
line: 7, | ||
column: 14, | ||
}, | ||
], | ||
}, | ||
]) | ||
), | ||
}); |
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.
Uh oh!
There was an error while loading. Please reload this page.