Skip to content

Commit 03eaeca

Browse files
committed
chore: recommend prefer-screen-queries
BREAKING CHANGE: recommend prefer-screen-queries
1 parent 185180a commit 03eaeca

File tree

4 files changed

+30
-12
lines changed

4 files changed

+30
-12
lines changed

README.md

+4-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,9 @@
2323
[![Tweet][tweet-badge]][tweet-url]
2424

2525
<!-- ALL-CONTRIBUTORS-BADGE:START - Do not remove or modify this section -->
26+
2627
[![All Contributors](https://img.shields.io/badge/all_contributors-24-orange.svg?style=flat-square)](#contributors-)
28+
2729
<!-- ALL-CONTRIBUTORS-BADGE:END -->
2830

2931
## Installation
@@ -145,7 +147,7 @@ To enable this configuration use the `extends` property in your
145147
| [prefer-explicit-assert](docs/rules/prefer-explicit-assert.md) | Suggest using explicit assertions rather than just `getBy*` queries | | |
146148
| [prefer-find-by](docs/rules/prefer-find-by.md) | Suggest using `findBy*` methods instead of the `waitFor` + `getBy` queries | ![recommended-badge][] ![angular-badge][] ![react-badge][] ![vue-badge][] | ![fixable-badge][] |
147149
| [prefer-presence-queries](docs/rules/prefer-presence-queries.md) | Enforce specific queries when checking element is present or not | | |
148-
| [prefer-screen-queries](docs/rules/prefer-screen-queries.md) | Suggest using screen while using queries | | |
150+
| [prefer-screen-queries](docs/rules/prefer-screen-queries.md) | Suggest using screen while using queries | ![recommended-badge][] ![angular-badge][] ![react-badge][] ![vue-badge][] | |
149151
| [prefer-wait-for](docs/rules/prefer-wait-for.md) | Use `waitFor` instead of deprecated wait methods | | ![fixable-badge][] |
150152

151153
[build-badge]: https://img.shields.io/travis/testing-library/eslint-plugin-testing-library?style=flat-square
@@ -212,6 +214,7 @@ Thanks goes to these wonderful people ([emoji key](https://allcontributors.org/d
212214

213215
<!-- markdownlint-enable -->
214216
<!-- prettier-ignore-end -->
217+
215218
<!-- ALL-CONTRIBUTORS-LIST:END -->
216219

217220
This project follows the [all-contributors](https://github.com/all-contributors/all-contributors) specification. Contributions of any kind welcome!

lib/index.ts

+1
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ const recommendedRules = {
3535
'testing-library/await-async-utils': 'error',
3636
'testing-library/no-await-sync-query': 'error',
3737
'testing-library/prefer-find-by': 'error',
38+
'testing-library/prefer-screen-queries': 'error',
3839
};
3940

4041
export = {

lib/rules/prefer-screen-queries.ts

+21-11
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ export default ESLintUtils.RuleCreator(getDocsUrl)<Options, MessageIds>({
2121
docs: {
2222
description: 'Suggest using screen while using queries',
2323
category: 'Best Practices',
24-
recommended: false,
24+
recommended: 'error',
2525
},
2626
messages: {
2727
preferScreenQueries:
@@ -46,28 +46,39 @@ export default ESLintUtils.RuleCreator(getDocsUrl)<Options, MessageIds>({
4646
const queriesRegex = new RegExp(ALL_QUERIES_COMBINATIONS_REGEXP);
4747
const queriesDestructuredInWithinDeclaration: string[] = [];
4848
// use an array as within might be used more than once in a test
49-
const withinDeclaredVariables : string[] = []
49+
const withinDeclaredVariables: string[] = [];
5050

5151
return {
5252
VariableDeclarator(node) {
53-
const isWithinFunction = isCallExpression(node.init) && isIdentifier(node.init.callee) && node.init.callee.name === 'within';
53+
const isWithinFunction =
54+
isCallExpression(node.init) &&
55+
isIdentifier(node.init.callee) &&
56+
node.init.callee.name === 'within';
5457

5558
if (!isWithinFunction) {
56-
return
59+
return;
5760
}
5861

5962
if (isObjectPattern(node.id)) {
6063
// save the destructured query methods
6164
const identifiers = node.id.properties
62-
.filter(property => isProperty(property) && isIdentifier(property.key) && queriesRegex.test(property.key.name))
63-
.map((property: TSESTree.Property) => (property.key as TSESTree.Identifier).name);
65+
.filter(
66+
property =>
67+
isProperty(property) &&
68+
isIdentifier(property.key) &&
69+
queriesRegex.test(property.key.name)
70+
)
71+
.map(
72+
(property: TSESTree.Property) =>
73+
(property.key as TSESTree.Identifier).name
74+
);
6475

6576
queriesDestructuredInWithinDeclaration.push(...identifiers);
66-
return
77+
return;
6778
}
6879

6980
if (isIdentifier(node.id)) {
70-
withinDeclaredVariables.push(node.id.name)
81+
withinDeclaredVariables.push(node.id.name);
7182
}
7283
},
7384
[`CallExpression > Identifier[name=/^${ALL_QUERIES_COMBINATIONS_REGEXP}$/]`](
@@ -84,16 +95,15 @@ export default ESLintUtils.RuleCreator(getDocsUrl)<Options, MessageIds>({
8495
[`MemberExpression > Identifier[name=/^${ALL_QUERIES_COMBINATIONS_REGEXP}$/]`](
8596
node: TSESTree.Identifier
8697
) {
87-
8898
function isIdentifierAllowed(name: string) {
89-
return ['screen', ...withinDeclaredVariables].includes(name)
99+
return ['screen', ...withinDeclaredVariables].includes(name);
90100
}
91101

92102
if (
93103
isIdentifier(node) &&
94104
isMemberExpression(node.parent) &&
95105
isCallExpression(node.parent.object) &&
96-
isIdentifier(node.parent.object.callee) &&
106+
isIdentifier(node.parent.object.callee) &&
97107
node.parent.object.callee.name !== 'within'
98108
) {
99109
reportInvalidUsage(node);

tests/__snapshots__/index.test.ts.snap

+4
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ Object {
1515
"angular",
1616
],
1717
"testing-library/prefer-find-by": "error",
18+
"testing-library/prefer-screen-queries": "error",
1819
},
1920
}
2021
`;
@@ -34,6 +35,7 @@ Object {
3435
"react",
3536
],
3637
"testing-library/prefer-find-by": "error",
38+
"testing-library/prefer-screen-queries": "error",
3739
},
3840
}
3941
`;
@@ -48,6 +50,7 @@ Object {
4850
"testing-library/await-async-utils": "error",
4951
"testing-library/no-await-sync-query": "error",
5052
"testing-library/prefer-find-by": "error",
53+
"testing-library/prefer-screen-queries": "error",
5154
},
5255
}
5356
`;
@@ -68,6 +71,7 @@ Object {
6871
"vue",
6972
],
7073
"testing-library/prefer-find-by": "error",
74+
"testing-library/prefer-screen-queries": "error",
7175
},
7276
}
7377
`;

0 commit comments

Comments
 (0)