Skip to content

Commit dc23733

Browse files
committed
fix: cr changes
1 parent 45c70c2 commit dc23733

File tree

3 files changed

+21
-11
lines changed

3 files changed

+21
-11
lines changed

docs/rules/no-test-id-queries.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,3 +29,4 @@ const input = screen.getByRole('textbox');
2929
- [about `getByTestId`](https://testing-library.com/docs/queries/bytestid)
3030
- [about `getByRole`](https://testing-library.com/docs/queries/byrole)
3131
- [about `getByLabelText`](https://testing-library.com/docs/queries/bylabeltext)
32+
- [Common mistakes with React Testing Library - Not querying by text](https://kentcdodds.com/blog/common-mistakes-with-react-testing-library#not-querying-by-text)

lib/rules/no-test-id-queries.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
import { TSESTree } from '@typescript-eslint/utils';
22

33
import { createTestingLibraryRule } from '../create-testing-library-rule';
4+
import { ALL_QUERIES_VARIANTS } from '../utils';
45

56
export const RULE_NAME = 'no-test-id-queries';
67
export type MessageIds = 'noTestIdQueries';
78
type Options = [];
89

9-
const QUERIES_REGEX = /^(get|query|getAll|queryAll|find|findAll)ByTestId$/;
10+
const QUERIES_REGEX = `/^(${ALL_QUERIES_VARIANTS.join('|')})TestId$/`;
1011

1112
export default createTestingLibraryRule<Options, MessageIds>({
1213
name: RULE_NAME,
@@ -33,7 +34,7 @@ export default createTestingLibraryRule<Options, MessageIds>({
3334

3435
create(context) {
3536
return {
36-
[`CallExpression[callee.property.name=${String(QUERIES_REGEX)}], CallExpression[callee.name=${String(QUERIES_REGEX)}]`](
37+
[`CallExpression[callee.property.name=${QUERIES_REGEX}], CallExpression[callee.name=${QUERIES_REGEX}]`](
3738
node: TSESTree.CallExpression
3839
) {
3940
context.report({

tests/lib/rules/no-test-id-queries.test.ts

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -22,17 +22,25 @@ const QUERIES = [
2222

2323
ruleTester.run(RULE_NAME, rule, {
2424
valid: [
25-
{
26-
code: `
27-
import { render } from '@testing-library/react';
25+
`
26+
import { render } from '@testing-library/react';
2827
29-
test('test', async () => {
30-
const { getByRole } = render(<MyComponent />);
28+
test('test', async () => {
29+
const { getByRole } = render(<MyComponent />);
3130
32-
expect(getByRole('button')).toBeInTheDocument();
33-
});
34-
`,
35-
},
31+
expect(getByRole('button')).toBeInTheDocument();
32+
});
33+
`,
34+
35+
`
36+
import { render } from '@testing-library/react';
37+
38+
test('test', async () => {
39+
render(<MyComponent />);
40+
41+
expect(getTestId('button')).toBeInTheDocument();
42+
});
43+
`,
3644
],
3745

3846
invalid: SUPPORTED_TESTING_FRAMEWORKS.flatMap((framework) =>

0 commit comments

Comments
 (0)