Skip to content

Commit 31516ad

Browse files
CodingItWrongobsoke
andauthoredMay 10, 2023
feat: add prefer-query-matchers rule (#750)
* feat: add prefer-query-matchers rule Co-authored-by: Dale Karp <dale.karp@testdouble.com> * test: cover prefer-query-matchers with test Co-authored-by: Dale Karp <dale.karp@testdouble.com> * feat: set default options to no configured entries to check for Co-authored-by: Dale Karp <dale.karp@testdouble.com> * docs: add query doc and supported rules table entry * style: prettify rule file * fix: do not include prefer-query-matchers by default * fix: failing test * fix: make generated tests more realistic AST * fix: comment out test names --------- Co-authored-by: Dale Karp <dale.karp@testdouble.com>
1 parent 3f2f4f8 commit 31516ad

File tree

7 files changed

+609
-1
lines changed

7 files changed

+609
-1
lines changed
 

‎README.md

+1
Original file line numberDiff line numberDiff line change
@@ -228,6 +228,7 @@ module.exports = {
228228
| [prefer-find-by](docs/rules/prefer-find-by.md) | Suggest using `find(All)By*` query instead of `waitFor` + `get(All)By*` to wait for elements | ![badge-angular][] ![badge-dom][] ![badge-marko][] ![badge-react][] ![badge-vue][] | 🔧 |
229229
| [prefer-presence-queries](docs/rules/prefer-presence-queries.md) | Ensure appropriate `get*`/`query*` queries are used with their respective matchers | ![badge-angular][] ![badge-dom][] ![badge-marko][] ![badge-react][] ![badge-vue][] | |
230230
| [prefer-query-by-disappearance](docs/rules/prefer-query-by-disappearance.md) | Suggest using `queryBy*` queries when waiting for disappearance | ![badge-angular][] ![badge-dom][] ![badge-marko][] ![badge-react][] ![badge-vue][] | |
231+
| [prefer-query-matchers](docs/rules/prefer-query-matchers.md) | Ensure the configured `get*`/`query*` query is used with the corresponding matchers | | |
231232
| [prefer-screen-queries](docs/rules/prefer-screen-queries.md) | Suggest using `screen` while querying | ![badge-angular][] ![badge-dom][] ![badge-marko][] ![badge-react][] ![badge-vue][] | |
232233
| [prefer-user-event](docs/rules/prefer-user-event.md) | Suggest using `userEvent` over `fireEvent` for simulating user interactions | | |
233234
| [prefer-wait-for](docs/rules/prefer-wait-for.md) | Use `waitFor` instead of deprecated wait methods | | 🔧 |

‎docs/rules/prefer-presence-queries.md

+1
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ Examples of **correct** code for this rule:
4343
```js
4444
test('some test', async () => {
4545
render(<App />);
46+
4647
// check element is present with `getBy*`
4748
expect(screen.getByText('button')).toBeInTheDocument();
4849
expect(screen.getAllByText('button')[9]).toBeTruthy();

‎docs/rules/prefer-query-matchers.md

+85
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
# Ensure the configured `get*`/`query*` query is used with the corresponding matchers (`testing-library/prefer-query-matchers`)
2+
3+
<!-- end auto-generated rule header -->
4+
5+
The (DOM) Testing Library allows to query DOM elements using different types of queries such as `get*` and `query*`. Using `get*` throws an error in case the element is not found, while `query*` returns null instead of throwing (or empty array for `queryAllBy*` ones).
6+
7+
It may be helpful to ensure that either `get*` or `query*` are always used for a given matcher. For example, `.toBeVisible()` and the negation `.not.toBeVisible()` both assume that an element exists in the DOM and will error if not. Using `get*` with `.toBeVisible()` ensures that if the element is not found the error thrown will offer better info than with `query*`.
8+
9+
## Rule details
10+
11+
This rule must be configured with a list of `validEntries`: for a given matcher, is `get*` or `query*` required.
12+
13+
Assuming the following configuration:
14+
15+
```json
16+
{
17+
"testing-library/prefer-query-matchers": [
18+
2,
19+
{
20+
"validEntries": [{ "matcher": "toBeVisible", "query": "get" }]
21+
}
22+
]
23+
}
24+
```
25+
26+
Examples of **incorrect** code for this rule with the above configuration:
27+
28+
```js
29+
test('some test', () => {
30+
render(<App />);
31+
32+
// use configured matcher with the disallowed `query*`
33+
expect(screen.queryByText('button')).toBeVisible();
34+
expect(screen.queryByText('button')).not.toBeVisible();
35+
expect(screen.queryAllByText('button')[0]).toBeVisible();
36+
expect(screen.queryAllByText('button')[0]).not.toBeVisible();
37+
});
38+
```
39+
40+
Examples of **correct** code for this rule:
41+
42+
```js
43+
test('some test', async () => {
44+
render(<App />);
45+
// use configured matcher with the allowed `get*`
46+
expect(screen.getByText('button')).toBeVisible();
47+
expect(screen.getByText('button')).not.toBeVisible();
48+
expect(screen.getAllByText('button')[0]).toBeVisible();
49+
expect(screen.getAllByText('button')[0]).not.toBeVisible();
50+
51+
// use an unconfigured matcher with either `get* or `query*
52+
expect(screen.getByText('button')).toBeEnabled();
53+
expect(screen.getAllByText('checkbox')[0]).not.toBeChecked();
54+
expect(screen.queryByText('button')).toHaveFocus();
55+
expect(screen.queryAllByText('button')[0]).not.toMatchMyCustomMatcher();
56+
57+
// `findBy*` queries are out of the scope for this rule
58+
const button = await screen.findByText('submit');
59+
expect(button).toBeVisible();
60+
});
61+
```
62+
63+
## Options
64+
65+
| Option | Required | Default | Details |
66+
| -------------- | -------- | ------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ |
67+
| `validEntries` | No | `[]` | A list of objects with a `matcher` property (the name of any matcher, such as "toBeVisible") and a `query` property (either "get" or "query"). Indicates whether `get*` or `query*` are allowed with this matcher. |
68+
69+
## Example
70+
71+
```json
72+
{
73+
"testing-library/prefer-query-matchers": [
74+
2,
75+
{
76+
"validEntries": [{ "matcher": "toBeVisible", "query": "get" }]
77+
}
78+
]
79+
}
80+
```
81+
82+
## Further Reading
83+
84+
- [Testing Library queries cheatsheet](https://testing-library.com/docs/dom-testing-library/cheatsheet#queries)
85+
- [jest-dom note about using `getBy` within assertions](https://testing-library.com/docs/ecosystem-jest-dom)

‎lib/create-testing-library-rule/detect-testing-library-utils.ts

+16
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,10 @@ type IsDebugUtilFn = (
8585
validNames?: ReadonlyArray<(typeof DEBUG_UTILS)[number]>
8686
) => boolean;
8787
type IsPresenceAssertFn = (node: TSESTree.MemberExpression) => boolean;
88+
type IsMatchingAssertFn = (
89+
node: TSESTree.MemberExpression,
90+
matcherName: string
91+
) => boolean;
8892
type IsAbsenceAssertFn = (node: TSESTree.MemberExpression) => boolean;
8993
type CanReportErrorsFn = () => boolean;
9094
type FindImportedTestingLibraryUtilSpecifierFn = (
@@ -122,6 +126,7 @@ export interface DetectionHelpers {
122126
isActUtil: (node: TSESTree.Identifier) => boolean;
123127
isPresenceAssert: IsPresenceAssertFn;
124128
isAbsenceAssert: IsAbsenceAssertFn;
129+
isMatchingAssert: IsMatchingAssertFn;
125130
canReportErrors: CanReportErrorsFn;
126131
findImportedTestingLibraryUtilSpecifier: FindImportedTestingLibraryUtilSpecifierFn;
127132
isNodeComingFromTestingLibrary: IsNodeComingFromTestingLibraryFn;
@@ -819,6 +824,16 @@ export function detectTestingLibraryUtils<
819824
: ABSENCE_MATCHERS.includes(matcher);
820825
};
821826

827+
const isMatchingAssert: IsMatchingAssertFn = (node, matcherName) => {
828+
const { matcher } = getAssertNodeInfo(node);
829+
830+
if (!matcher) {
831+
return false;
832+
}
833+
834+
return matcher === matcherName;
835+
};
836+
822837
/**
823838
* Finds the import util specifier related to Testing Library for a given name.
824839
*/
@@ -977,6 +992,7 @@ export function detectTestingLibraryUtils<
977992
isDebugUtil,
978993
isActUtil,
979994
isPresenceAssert,
995+
isMatchingAssert,
980996
isAbsenceAssert,
981997
canReportErrors,
982998
findImportedTestingLibraryUtilSpecifier,

‎lib/rules/prefer-query-matchers.ts

+105
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
import { TSESTree } from '@typescript-eslint/utils';
2+
3+
import { createTestingLibraryRule } from '../create-testing-library-rule';
4+
import { findClosestCallNode, isMemberExpression } from '../node-utils';
5+
6+
export const RULE_NAME = 'prefer-query-matchers';
7+
export type MessageIds = 'wrongQueryForMatcher';
8+
export type Options = [
9+
{
10+
validEntries: {
11+
query: 'get' | 'query';
12+
matcher: string;
13+
}[];
14+
}
15+
];
16+
17+
export default createTestingLibraryRule<Options, MessageIds>({
18+
name: RULE_NAME,
19+
meta: {
20+
docs: {
21+
description:
22+
'Ensure the configured `get*`/`query*` query is used with the corresponding matchers',
23+
recommendedConfig: {
24+
dom: false,
25+
angular: false,
26+
react: false,
27+
vue: false,
28+
marko: false,
29+
},
30+
},
31+
messages: {
32+
wrongQueryForMatcher: 'Use `{{ query }}By*` queries for {{ matcher }}',
33+
},
34+
schema: [
35+
{
36+
type: 'object',
37+
additionalProperties: false,
38+
properties: {
39+
validEntries: {
40+
type: 'array',
41+
items: {
42+
type: 'object',
43+
properties: {
44+
query: {
45+
type: 'string',
46+
enum: ['get', 'query'],
47+
},
48+
matcher: {
49+
type: 'string',
50+
},
51+
},
52+
},
53+
},
54+
},
55+
},
56+
],
57+
type: 'suggestion',
58+
},
59+
defaultOptions: [
60+
{
61+
validEntries: [],
62+
},
63+
],
64+
65+
create(context, [{ validEntries }], helpers) {
66+
return {
67+
'CallExpression Identifier'(node: TSESTree.Identifier) {
68+
const expectCallNode = findClosestCallNode(node, 'expect');
69+
70+
if (!expectCallNode || !isMemberExpression(expectCallNode.parent)) {
71+
return;
72+
}
73+
74+
// Sync queries (getBy and queryBy) and corresponding ones
75+
// are supported. If none found, stop the rule.
76+
if (!helpers.isSyncQuery(node)) {
77+
return;
78+
}
79+
80+
const isGetBy = helpers.isGetQueryVariant(node);
81+
const expectStatement = expectCallNode.parent;
82+
for (const entry of validEntries) {
83+
const { query, matcher } = entry;
84+
const isMatchingAssertForThisEntry = helpers.isMatchingAssert(
85+
expectStatement,
86+
matcher
87+
);
88+
89+
if (!isMatchingAssertForThisEntry) {
90+
continue;
91+
}
92+
93+
const actualQuery = isGetBy ? 'get' : 'query';
94+
if (query !== actualQuery) {
95+
context.report({
96+
node,
97+
messageId: 'wrongQueryForMatcher',
98+
data: { query, matcher },
99+
});
100+
}
101+
}
102+
},
103+
};
104+
},
105+
});

‎tests/index.test.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { resolve } from 'path';
33

44
import plugin from '../lib';
55

6-
const numberOfRules = 27;
6+
const numberOfRules = 28;
77
const ruleNames = Object.keys(plugin.rules);
88

99
// eslint-disable-next-line jest/expect-expect
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,400 @@
1+
import { TSESLint } from '@typescript-eslint/utils';
2+
3+
import rule, {
4+
RULE_NAME,
5+
MessageIds,
6+
Options,
7+
} from '../../../lib/rules/prefer-query-matchers';
8+
import { ALL_QUERIES_METHODS } from '../../../lib/utils';
9+
import { createRuleTester } from '../test-utils';
10+
11+
const ruleTester = createRuleTester();
12+
13+
const getByQueries = ALL_QUERIES_METHODS.map((method) => `get${method}`);
14+
const getAllByQueries = ALL_QUERIES_METHODS.map((method) => `getAll${method}`);
15+
const queryByQueries = ALL_QUERIES_METHODS.map((method) => `query${method}`);
16+
const queryAllByQueries = ALL_QUERIES_METHODS.map(
17+
(method) => `queryAll${method}`
18+
);
19+
20+
type RuleValidTestCase = TSESLint.ValidTestCase<Options>;
21+
type RuleInvalidTestCase = TSESLint.InvalidTestCase<MessageIds, Options>;
22+
23+
type AssertionFnParams = {
24+
query: string;
25+
matcher: string;
26+
options: Options;
27+
};
28+
29+
const wrapExpectInTest = (expectStatement: string) => `
30+
import { render, screen } from '@testing-library/react'
31+
32+
test('a fake test', () => {
33+
render(<Component />)
34+
35+
${expectStatement}
36+
})`;
37+
38+
const getValidAssertions = ({
39+
query,
40+
matcher,
41+
options,
42+
}: AssertionFnParams): RuleValidTestCase[] => {
43+
const expectStatement = `expect(${query}('Hello'))${matcher}`;
44+
const expectScreenStatement = `expect(screen.${query}('Hello'))${matcher}`;
45+
return [
46+
{
47+
// name: `${expectStatement} with default options of empty validEntries`,
48+
code: wrapExpectInTest(expectStatement),
49+
},
50+
{
51+
// name: `${expectStatement} with provided options`,
52+
code: wrapExpectInTest(expectStatement),
53+
options,
54+
},
55+
{
56+
// name: `${expectScreenStatement} with default options of empty validEntries`,
57+
code: wrapExpectInTest(expectScreenStatement),
58+
},
59+
{
60+
// name: `${expectScreenStatement} with provided options`,
61+
code: wrapExpectInTest(expectScreenStatement),
62+
options,
63+
},
64+
];
65+
};
66+
67+
const getInvalidAssertions = ({
68+
query,
69+
matcher,
70+
options,
71+
}: AssertionFnParams): RuleInvalidTestCase[] => {
72+
const expectStatement = `expect(${query}('Hello'))${matcher}`;
73+
const expectScreenStatement = `expect(screen.${query}('Hello'))${matcher}`;
74+
const messageId: MessageIds = 'wrongQueryForMatcher';
75+
const [
76+
{
77+
validEntries: [validEntry],
78+
},
79+
] = options;
80+
return [
81+
{
82+
// name: `${expectStatement} with provided options`,
83+
code: wrapExpectInTest(expectStatement),
84+
options,
85+
errors: [
86+
{
87+
messageId,
88+
line: 7,
89+
column: 10,
90+
data: { query: validEntry.query, matcher: validEntry.matcher },
91+
},
92+
],
93+
},
94+
{
95+
// name: `${expectScreenStatement} with provided options`,
96+
code: wrapExpectInTest(expectScreenStatement),
97+
options,
98+
errors: [
99+
{
100+
messageId,
101+
line: 7,
102+
column: 17,
103+
data: { query: validEntry.query, matcher: validEntry.matcher },
104+
},
105+
],
106+
},
107+
];
108+
};
109+
110+
ruleTester.run(RULE_NAME, rule, {
111+
valid: [
112+
// cases: methods not matching Testing Library queries pattern
113+
`expect(queryElement('foo')).toBeVisible()`,
114+
`expect(getElement('foo')).not.toBeVisible()`,
115+
// cases: asserting with a configured allowed `[screen.]getBy*` query
116+
...getByQueries.reduce<RuleValidTestCase[]>(
117+
(validRules, queryName) => [
118+
...validRules,
119+
...getValidAssertions({
120+
query: queryName,
121+
matcher: '.toBeVisible()',
122+
options: [
123+
{ validEntries: [{ query: 'get', matcher: 'toBeVisible' }] },
124+
],
125+
}),
126+
...getValidAssertions({
127+
query: queryName,
128+
matcher: '.toBeHelloWorld()',
129+
options: [
130+
{ validEntries: [{ query: 'get', matcher: 'toBeVisible' }] },
131+
],
132+
}),
133+
...getValidAssertions({
134+
query: queryName,
135+
matcher: '.not.toBeVisible()',
136+
options: [
137+
{ validEntries: [{ query: 'get', matcher: 'toBeVisible' }] },
138+
],
139+
}),
140+
...getValidAssertions({
141+
query: queryName,
142+
matcher: '.not.toBeHelloWorld()',
143+
options: [
144+
{ validEntries: [{ query: 'get', matcher: 'toBeVisible' }] },
145+
],
146+
}),
147+
...getValidAssertions({
148+
query: queryName,
149+
matcher: '.toBeHelloWorld()',
150+
options: [
151+
{ validEntries: [{ query: 'query', matcher: 'toBeVisible' }] },
152+
],
153+
}),
154+
],
155+
[]
156+
),
157+
// cases: asserting with a configured allowed `[screen.]getAllBy*` query
158+
...getAllByQueries.reduce<RuleValidTestCase[]>(
159+
(validRules, queryName) => [
160+
...validRules,
161+
...getValidAssertions({
162+
query: queryName,
163+
matcher: '.toBeVisible()',
164+
options: [
165+
{ validEntries: [{ query: 'get', matcher: 'toBeVisible' }] },
166+
],
167+
}),
168+
...getValidAssertions({
169+
query: queryName,
170+
matcher: '.toBeHelloWorld()',
171+
options: [
172+
{ validEntries: [{ query: 'get', matcher: 'toBeVisible' }] },
173+
],
174+
}),
175+
...getValidAssertions({
176+
query: queryName,
177+
matcher: '.not.toBeVisible()',
178+
options: [
179+
{ validEntries: [{ query: 'get', matcher: 'toBeVisible' }] },
180+
],
181+
}),
182+
...getValidAssertions({
183+
query: queryName,
184+
matcher: '.not.toBeHelloWorld()',
185+
options: [
186+
{ validEntries: [{ query: 'get', matcher: 'toBeVisible' }] },
187+
],
188+
}),
189+
...getValidAssertions({
190+
query: queryName,
191+
matcher: '.toBeHelloWorld()',
192+
options: [
193+
{ validEntries: [{ query: 'query', matcher: 'toBeVisible' }] },
194+
],
195+
}),
196+
],
197+
[]
198+
),
199+
// cases: asserting with a configured allowed `[screen.]queryBy*` query
200+
...queryByQueries.reduce<RuleValidTestCase[]>(
201+
(validRules, queryName) => [
202+
...validRules,
203+
...getValidAssertions({
204+
query: queryName,
205+
matcher: '.toBeVisible()',
206+
options: [
207+
{ validEntries: [{ query: 'query', matcher: 'toBeVisible' }] },
208+
],
209+
}),
210+
...getValidAssertions({
211+
query: queryName,
212+
matcher: '.toBeHelloWorld()',
213+
options: [
214+
{ validEntries: [{ query: 'query', matcher: 'toBeVisible' }] },
215+
],
216+
}),
217+
...getValidAssertions({
218+
query: queryName,
219+
matcher: '.not.toBeVisible()',
220+
options: [
221+
{ validEntries: [{ query: 'query', matcher: 'toBeVisible' }] },
222+
],
223+
}),
224+
...getValidAssertions({
225+
query: queryName,
226+
matcher: '.not.toBeHelloWorld()',
227+
options: [
228+
{ validEntries: [{ query: 'query', matcher: 'toBeVisible' }] },
229+
],
230+
}),
231+
...getValidAssertions({
232+
query: queryName,
233+
matcher: '.toBeHelloWorld()',
234+
options: [
235+
{ validEntries: [{ query: 'get', matcher: 'toBeVisible' }] },
236+
],
237+
}),
238+
],
239+
[]
240+
),
241+
// cases: asserting with a configured allowed `[screen.]queryAllBy*` query
242+
...queryAllByQueries.reduce<RuleValidTestCase[]>(
243+
(validRules, queryName) => [
244+
...validRules,
245+
...getValidAssertions({
246+
query: queryName,
247+
matcher: '.toBeVisible()',
248+
options: [
249+
{ validEntries: [{ query: 'query', matcher: 'toBeVisible' }] },
250+
],
251+
}),
252+
...getValidAssertions({
253+
query: queryName,
254+
matcher: '.toBeHelloWorld()',
255+
options: [
256+
{ validEntries: [{ query: 'query', matcher: 'toBeVisible' }] },
257+
],
258+
}),
259+
...getValidAssertions({
260+
query: queryName,
261+
matcher: '.not.toBeVisible()',
262+
options: [
263+
{ validEntries: [{ query: 'query', matcher: 'toBeVisible' }] },
264+
],
265+
}),
266+
...getValidAssertions({
267+
query: queryName,
268+
matcher: '.not.toBeHelloWorld()',
269+
options: [
270+
{ validEntries: [{ query: 'query', matcher: 'toBeVisible' }] },
271+
],
272+
}),
273+
...getValidAssertions({
274+
query: queryName,
275+
matcher: '.toBeHelloWorld()',
276+
options: [
277+
{ validEntries: [{ query: 'get', matcher: 'toBeVisible' }] },
278+
],
279+
}),
280+
],
281+
[]
282+
),
283+
// case: getting outside an expectation
284+
{
285+
code: 'const el = getByText("button")',
286+
},
287+
// case: querying outside an expectation
288+
{
289+
code: 'const el = queryByText("button")',
290+
},
291+
// case: finding outside an expectation
292+
{
293+
code: `async () => {
294+
const el = await findByText('button')
295+
expect(el).toBeVisible()
296+
}`,
297+
},
298+
{
299+
code: `// case: query an element with getBy but then check its absence after doing
300+
// some action which makes it disappear.
301+
302+
// submit button exists
303+
const submitButton = screen.getByRole('button')
304+
fireEvent.click(submitButton)
305+
306+
// right after clicking submit button it disappears
307+
expect(submitButton).toBeHelloWorld()
308+
`,
309+
options: [
310+
{ validEntries: [{ query: 'query', matcher: 'toBeHelloWorld' }] },
311+
],
312+
},
313+
],
314+
invalid: [
315+
// cases: asserting with a disallowed `[screen.]getBy*` query
316+
...getByQueries.reduce<RuleInvalidTestCase[]>(
317+
(validRules, queryName) => [
318+
...validRules,
319+
...getInvalidAssertions({
320+
query: queryName,
321+
matcher: '.toBeHelloWorld()',
322+
options: [
323+
{ validEntries: [{ query: 'query', matcher: 'toBeHelloWorld' }] },
324+
],
325+
}),
326+
],
327+
[]
328+
),
329+
// cases: asserting with a disallowed `[screen.]getAllBy*` query
330+
...getAllByQueries.reduce<RuleInvalidTestCase[]>(
331+
(validRules, queryName) => [
332+
...validRules,
333+
...getInvalidAssertions({
334+
query: queryName,
335+
matcher: '.toBeHelloWorld()',
336+
options: [
337+
{ validEntries: [{ query: 'query', matcher: 'toBeHelloWorld' }] },
338+
],
339+
}),
340+
],
341+
[]
342+
),
343+
// cases: asserting with a disallowed `[screen.]getBy*` query
344+
...queryByQueries.reduce<RuleInvalidTestCase[]>(
345+
(invalidRules, queryName) => [
346+
...invalidRules,
347+
...getInvalidAssertions({
348+
query: queryName,
349+
matcher: '.toBeVisible()',
350+
options: [
351+
{ validEntries: [{ query: 'get', matcher: 'toBeVisible' }] },
352+
],
353+
}),
354+
],
355+
[]
356+
),
357+
// cases: asserting with a disallowed `[screen.]queryAllBy*` query
358+
...queryAllByQueries.reduce<RuleInvalidTestCase[]>(
359+
(invalidRules, queryName) => [
360+
...invalidRules,
361+
...getInvalidAssertions({
362+
query: queryName,
363+
matcher: '.toBeVisible()',
364+
options: [
365+
{ validEntries: [{ query: 'get', matcher: 'toBeVisible' }] },
366+
],
367+
}),
368+
],
369+
[]
370+
),
371+
// cases: indexing into an `AllBy` result within the expectation
372+
{
373+
code: 'expect(screen.queryAllByText("button")[1]).toBeVisible()',
374+
options: [{ validEntries: [{ query: 'get', matcher: 'toBeVisible' }] }],
375+
errors: [
376+
{
377+
messageId: 'wrongQueryForMatcher',
378+
line: 1,
379+
column: 15,
380+
data: { query: 'get', matcher: 'toBeVisible' },
381+
},
382+
],
383+
},
384+
{
385+
code: `
386+
// case: asserting presence incorrectly with custom queryBy* query
387+
expect(queryByCustomQuery("button")).toBeVisible()
388+
`,
389+
options: [{ validEntries: [{ query: 'get', matcher: 'toBeVisible' }] }],
390+
errors: [
391+
{
392+
messageId: 'wrongQueryForMatcher',
393+
line: 3,
394+
column: 12,
395+
data: { query: 'get', matcher: 'toBeVisible' },
396+
},
397+
],
398+
},
399+
],
400+
});

0 commit comments

Comments
 (0)
Please sign in to comment.