-
Notifications
You must be signed in to change notification settings - Fork 150
feat(rule): prefer expect queryBy #22
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
Changes from 1 commit
62ed216
353cc77
093b44b
e3e4231
53a950c
0fd4a6c
6a3f264
67d305c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
# Disallow the use of `expect(getBy*)` (prefer-expect-query-by) | ||
|
||
The (DOM) Testing Library support two 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. | ||
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 `expect(queryByText("Foo")).not.toBeInTheDocument()`. | ||
|
||
## Rule details | ||
|
||
This rule gives a notification whenever `expect(getBy*)` is used. | ||
|
||
This rule is enabled by default. | ||
|
||
### Default configuration | ||
|
||
The following patterns is considered erroneous: | ||
|
||
```js | ||
test('some test', () => { | ||
expect(getByText('Foo')).not.toBeInTheDocument(); | ||
}); | ||
``` | ||
|
||
The following pattern is considered non erroneous: | ||
|
||
```js | ||
test('some test', async () => { | ||
expect(queryByText('Foo')).not.toBeInTheDocument(); | ||
}); | ||
``` | ||
|
||
## Further Reading | ||
|
||
- [Appearance and Disappearance](https://testing-library.com/docs/guide-disappearance#asserting-elements-are-not-present) | ||
- [Testing Library queries cheatsheet](https://testing-library.com/docs/dom-testing-library/cheatsheet#queries) |
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
@@ -0,0 +1,99 @@ | ||||||
'use strict'; | ||||||
|
||||||
const { getDocsUrl } = require('../utils'); | ||||||
|
||||||
const AST_NODE_TYPES = { | ||||||
Belco90 marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
Identifier: 'Identifier', | ||||||
MemberExpression: 'MemberExpression', | ||||||
}; | ||||||
|
||||||
function isIdentifier(node) { | ||||||
return node.type === AST_NODE_TYPES.Identifier; | ||||||
} | ||||||
|
||||||
function isMemberExpression(node) { | ||||||
return node.type === AST_NODE_TYPES.MemberExpression; | ||||||
} | ||||||
|
||||||
function isUsingWrongQueries(node) { | ||||||
return node.name.startsWith('getBy') || node.name.startsWith('getAllBy'); | ||||||
} | ||||||
|
||||||
function isNotNullOrUndefined(input) { | ||||||
return input != null; | ||||||
} | ||||||
|
||||||
function mapNodesForWrongGetByQuery(node) { | ||||||
const nodeArguments = node.arguments; | ||||||
return nodeArguments | ||||||
.map(arg => { | ||||||
if (!arg.callee) { | ||||||
return null; | ||||||
} | ||||||
// Example: `expect(rendered.getBy*)` | ||||||
Belco90 marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
if (isMemberExpression(arg.callee)) { | ||||||
const node = arg.callee.property; | ||||||
if (isIdentifier(node) && isUsingWrongQueries(node)) { | ||||||
return node; | ||||||
} | ||||||
return null; | ||||||
} | ||||||
|
||||||
// Example: `expect(getBy*)` | ||||||
if (isIdentifier(arg.callee) && isUsingWrongQueries(arg.callee)) { | ||||||
return arg.callee; | ||||||
} | ||||||
|
||||||
return null; | ||||||
}) | ||||||
.filter(isNotNullOrUndefined); | ||||||
} | ||||||
|
||||||
function hasExpectWithWrongGetByQuery(node) { | ||||||
if ( | ||||||
node.callee && | ||||||
node.callee.type === AST_NODE_TYPES.Identifier && | ||||||
node.callee.name === 'expect' && | ||||||
node.arguments | ||||||
) { | ||||||
const nodesGetBy = mapNodesForWrongGetByQuery(node); | ||||||
return nodesGetBy.length > 0; | ||||||
} | ||||||
return false; | ||||||
} | ||||||
|
||||||
module.exports = { | ||||||
meta: { | ||||||
docs: { | ||||||
category: 'Best Practices', | ||||||
description: 'Disallow using getBy* queries in expect calls', | ||||||
recommended: 'error', | ||||||
url: getDocsUrl('prefer-expect-query-by'), | ||||||
}, | ||||||
messages: { | ||||||
expectQueryBy: | ||||||
'Using `expect(getBy*)` is not recommended, use `expect(queryBy*)` instead.', | ||||||
}, | ||||||
schema: [], | ||||||
type: 'suggestion', | ||||||
fixable: 'code', | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ah thanks, I missed that |
||||||
}, | ||||||
|
||||||
create: context => ({ | ||||||
CallExpression(node) { | ||||||
if (hasExpectWithWrongGetByQuery(node)) { | ||||||
const nodesGetBy = mapNodesForWrongGetByQuery(node); | ||||||
context.report({ | ||||||
node: node.callee, | ||||||
messageId: 'expectQueryBy', | ||||||
fix(fixer) { | ||||||
return fixer.replaceText( | ||||||
nodesGetBy[0], | ||||||
nodesGetBy[0].name.replace(/^(get(All)?(.*))$/, 'query$2$3') | ||||||
); | ||||||
}, | ||||||
}); | ||||||
} | ||||||
}, | ||||||
}), | ||||||
}; |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Uh oh!
There was an error while loading. Please reload this page.