Skip to content

Commit e3e4231

Browse files
committed
refactor: remove occurrences of findBy
1 parent 093b44b commit e3e4231

File tree

3 files changed

+5
-33
lines changed

3 files changed

+5
-33
lines changed

docs/rules/prefer-expect-query-by.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
# Disallow the use of `expect(getBy*)` (prefer-expect-query-by)
22

3-
The (DOM) Testing Library support three types of queries: `getBy*`, `findBy*` and `queryBy*`. Using `getBy*` or `findBy*` throws an error in case the element is not found. This is useful when using method like `waitForElement`, which are `async` functions that will wait for the element to be found until a certain timeout, after that the test will fail.
4-
However, when trying to assert if an element is not in the document, we can't use `getBy*` or `findBy*` as the test will fail immediately. Instead it is recommended to use `queryBy*`, which does not throw and therefore we can assert that e.g. `expect(queryByText("Foo")).not.toBeInTheDocument()`.
3+
The (DOM) Testing Library support three types of queries: `getBy*` and `queryBy*`. Using `getBy*` throws an error in case the element is not found. This is useful when using method like `waitForElement`, which are `async` functions that will wait for the element to be found until a certain timeout, after that the test will fail.
4+
However, when trying to assert if an element is not in the document, we can't use `getBy*` as the test will fail immediately. Instead it is recommended to use `queryBy*`, which does not throw and therefore we can assert that e.g. `expect(queryByText("Foo")).not.toBeInTheDocument()`.
55

6-
> The same applies for the `getAll*`, `findAll*` and `queryAll*` queries.
6+
> The same applies for the `getAll*` and `queryAll*` queries.
77
88
## Rule details
99

lib/rules/prefer-expect-query-by.js

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,7 @@ function isMemberExpression(node) {
1616
}
1717

1818
function isUsingWrongQueries(node) {
19-
return (
20-
node.name.startsWith('getBy') ||
21-
node.name.startsWith('getAllBy') ||
22-
node.name.startsWith('findBy') ||
23-
node.name.startsWith('findAllBy')
24-
);
19+
return node.name.startsWith('getBy') || node.name.startsWith('getAllBy');
2520
}
2621

2722
function isNotNullOrUndefined(input) {
@@ -94,10 +89,7 @@ module.exports = {
9489
fix(fixer) {
9590
return fixer.replaceText(
9691
nodesGetBy[0],
97-
nodesGetBy[0].name.replace(
98-
/^((get|find)(All)?(.*))$/,
99-
'query$3$4'
100-
)
92+
nodesGetBy[0].name.replace(/^(get(All)?(.*))$/, 'query$2$3')
10193
);
10294
},
10395
});

tests/lib/rules/prefer-expect-query-by.js

Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -64,25 +64,5 @@ ruleTester.run('prefer-expect-query-by', rule, {
6464
errors: [{ messageId: 'expectQueryBy' }],
6565
output: "expect(rendered.queryAllByText('Hello')).toBeInTheDocument()",
6666
},
67-
{
68-
code: "expect(findByText('Hello')).toBeInTheDocument()",
69-
errors: [{ messageId: 'expectQueryBy' }],
70-
output: "expect(queryByText('Hello')).toBeInTheDocument()",
71-
},
72-
{
73-
code: "expect(rendered.findByText('Hello')).toBeInTheDocument()",
74-
errors: [{ messageId: 'expectQueryBy' }],
75-
output: "expect(rendered.queryByText('Hello')).toBeInTheDocument()",
76-
},
77-
{
78-
code: "expect(findAllByText('Hello')).toBeInTheDocument()",
79-
errors: [{ messageId: 'expectQueryBy' }],
80-
output: "expect(queryAllByText('Hello')).toBeInTheDocument()",
81-
},
82-
{
83-
code: "expect(rendered.findAllByText('Hello')).toBeInTheDocument()",
84-
errors: [{ messageId: 'expectQueryBy' }],
85-
output: "expect(rendered.queryAllByText('Hello')).toBeInTheDocument()",
86-
},
8767
],
8868
});

0 commit comments

Comments
 (0)