Skip to content

Commit 1d8077c

Browse files
author
Mateus Felix
committed
2 parents c11fcd6 + d8a2ac6 commit 1d8077c

File tree

5 files changed

+35
-15
lines changed

5 files changed

+35
-15
lines changed

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424

2525
<!-- ALL-CONTRIBUTORS-BADGE:START - Do not remove or modify this section -->
2626

27-
[![All Contributors](https://img.shields.io/badge/all_contributors-26-orange.svg?style=flat-square)](#contributors-)
27+
[![All Contributors](https://img.shields.io/badge/all_contributors-24-orange.svg?style=flat-square)](#contributors-)
2828

2929
<!-- ALL-CONTRIBUTORS-BADGE:END -->
3030

@@ -144,11 +144,11 @@ To enable this configuration use the `extends` property in your
144144
| [no-debug](docs/rules/no-debug.md) | Disallow the use of `debug` | ![angular-badge][] ![react-badge][] ![vue-badge][] | |
145145
| [no-dom-import](docs/rules/no-dom-import.md) | Disallow importing from DOM Testing Library | ![angular-badge][] ![react-badge][] ![vue-badge][] | ![fixable-badge][] |
146146
| [no-manual-cleanup](docs/rules/no-manual-cleanup.md) | Disallow the use of `cleanup` | | |
147-
| [no-wait-for-empty-callback](docs/rules/no-wait-for-empty-callback.md) | Disallow empty callbacks for `waitFor` and `waitForElementToBeRemoved` | | |
147+
| [no-wait-for-empty-callback](docs/rules/no-wait-for-empty-callback.md) | Disallow empty callbacks for `waitFor` and `waitForElementToBeRemoved` | ![recommended-badge][] ![angular-badge][] ![react-badge][] ![vue-badge][] | |
148148
| [prefer-explicit-assert](docs/rules/prefer-explicit-assert.md) | Suggest using explicit assertions rather than just `getBy*` queries | | |
149149
| [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][] |
150150
| [prefer-presence-queries](docs/rules/prefer-presence-queries.md) | Enforce specific queries when checking element is present or not | | |
151-
| [prefer-screen-queries](docs/rules/prefer-screen-queries.md) | Suggest using screen while using queries | | |
151+
| [prefer-screen-queries](docs/rules/prefer-screen-queries.md) | Suggest using screen while using queries | ![recommended-badge][] ![angular-badge][] ![react-badge][] ![vue-badge][] | |
152152
| [prefer-wait-for](docs/rules/prefer-wait-for.md) | Use `waitFor` instead of deprecated wait methods | | ![fixable-badge][] |
153153

154154
[build-badge]: https://img.shields.io/travis/testing-library/eslint-plugin-testing-library?style=flat-square

lib/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,9 @@ const recommendedRules = {
3737
'testing-library/await-async-utils': 'error',
3838
'testing-library/no-await-sync-query': 'error',
3939
'testing-library/no-container': 'error',
40+
'testing-library/no-wait-for-empty-callback': 'error',
4041
'testing-library/prefer-find-by': 'error',
42+
'testing-library/prefer-screen-queries': 'error',
4143
};
4244

4345
export = {

lib/rules/no-wait-for-empty-callback.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ export default ESLintUtils.RuleCreator(getDocsUrl)<Options, MessageIds>({
2121
description:
2222
"It's preferred to avoid empty callbacks in `waitFor` and `waitForElementToBeRemoved`",
2323
category: 'Best Practices',
24-
recommended: false,
24+
recommended: 'error',
2525
},
2626
messages: {
2727
noWaitForEmptyCallback:

lib/rules/prefer-screen-queries.ts

Lines changed: 21 additions & 11 deletions
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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,9 @@ Object {
1515
"error",
1616
"angular",
1717
],
18+
"testing-library/no-wait-for-empty-callback": "error",
1819
"testing-library/prefer-find-by": "error",
20+
"testing-library/prefer-screen-queries": "error",
1921
},
2022
}
2123
`;
@@ -35,7 +37,9 @@ Object {
3537
"error",
3638
"react",
3739
],
40+
"testing-library/no-wait-for-empty-callback": "error",
3841
"testing-library/prefer-find-by": "error",
42+
"testing-library/prefer-screen-queries": "error",
3943
},
4044
}
4145
`;
@@ -50,7 +54,9 @@ Object {
5054
"testing-library/await-async-utils": "error",
5155
"testing-library/no-await-sync-query": "error",
5256
"testing-library/no-container": "error",
57+
"testing-library/no-wait-for-empty-callback": "error",
5358
"testing-library/prefer-find-by": "error",
59+
"testing-library/prefer-screen-queries": "error",
5460
},
5561
}
5662
`;
@@ -71,7 +77,9 @@ Object {
7177
"error",
7278
"vue",
7379
],
80+
"testing-library/no-wait-for-empty-callback": "error",
7481
"testing-library/prefer-find-by": "error",
82+
"testing-library/prefer-screen-queries": "error",
7583
},
7684
}
7785
`;

0 commit comments

Comments
 (0)