Skip to content

Commit c100330

Browse files
committed
feat(prefer-screen-queries): add prefer-screen-queries
Closes #95
1 parent 4e8003d commit c100330

File tree

6 files changed

+122
-2
lines changed

6 files changed

+122
-2
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,7 @@ To enable this configuration use the `extends` property in your
148148
| [no-wait-for-empty-callback](docs/rules/no-wait-for-empty-callback.md) | Disallow empty callbacks for `waitFor` and `waitForElementToBeRemoved` | | |
149149
| [prefer-explicit-assert](docs/rules/prefer-explicit-assert.md) | Suggest using explicit assertions rather than just `getBy*` queries | | |
150150
| [prefer-wait-for](docs/rules/prefer-wait-for.md) | Use `waitFor` instead of deprecated wait methods | | ![fixable-badge][] |
151+
| [prefer-screen-queries](docs/rules/prefer-screen-queries.md) | Suggest using screen while using queries | | |
151152

152153
[build-badge]: https://img.shields.io/travis/testing-library/eslint-plugin-testing-library?style=flat-square
153154
[build-url]: https://travis-ci.org/testing-library/eslint-plugin-testing-library

docs/rules/prefer-screen-queries.md

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
# Suggest using screen while using queries (prefer-screen-queries)
2+
3+
## Rule Details
4+
5+
This works better with autocomplete and makes each test a little simpler.
6+
7+
Examples of **incorrect** code for this rule:
8+
9+
```js
10+
// calling a query from the `render` method
11+
const { getByText } = render(<Component />);
12+
getByText('foo');
13+
14+
const utils = render(<Component />);
15+
utils.getByText('foo');
16+
```
17+
18+
Examples of **correct** code for this rule:
19+
20+
```js
21+
import { screen } from '@testing-library/any-framework';
22+
23+
screen.getByText('foo');
24+
```
25+
26+
## Further Reading
27+
28+
- [`screen` documentation](https://testing-library.com/docs/dom-testing-library/api-queries#screen)

lib/index.js

+1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ const rules = {
1313
'no-wait-for-empty-callback': require('./rules/no-wait-for-empty-callback'),
1414
'prefer-explicit-assert': require('./rules/prefer-explicit-assert'),
1515
'prefer-wait-for': require('./rules/prefer-wait-for'),
16+
'prefer-screen-queries': require('./rules/prefer-screen-queries'),
1617
};
1718

1819
const recommendedRules = {

lib/rules/prefer-screen-queries.js

+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
'use strict';
2+
3+
const { getDocsUrl, ALL_QUERIES_COMBINATIONS } = require('../utils');
4+
5+
const ALL_QUERIES_COMBINATIONS_REGEXP = ALL_QUERIES_COMBINATIONS.join('|');
6+
7+
module.exports = {
8+
meta: {
9+
type: 'suggestion',
10+
docs: {
11+
description: 'Suggest using screen while using queries',
12+
category: 'Best Practices',
13+
recommended: false,
14+
url: getDocsUrl('prefer-screen-queries'),
15+
},
16+
messages: {
17+
preferScreenQueries:
18+
'Use screen to query DOM elements, `screen.{{ name }}`',
19+
},
20+
fixable: null,
21+
schema: [],
22+
},
23+
24+
create: function(context) {
25+
function reportInvalidUsage(node) {
26+
context.report({
27+
node,
28+
messageId: 'preferScreenQueries',
29+
data: {
30+
name: node.name,
31+
},
32+
});
33+
}
34+
35+
return {
36+
[`CallExpression > Identifier[name=/^${ALL_QUERIES_COMBINATIONS_REGEXP}$/]`]: reportInvalidUsage,
37+
[`MemberExpression[object.name!="screen"] > Identifier[name=/^${ALL_QUERIES_COMBINATIONS_REGEXP}$/]`]: reportInvalidUsage,
38+
};
39+
},
40+
};

lib/utils.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,8 @@ const ASYNC_QUERIES_COMBINATIONS = combineQueries(
4444
);
4545

4646
const ALL_QUERIES_COMBINATIONS = [
47-
SYNC_QUERIES_COMBINATIONS,
48-
ASYNC_QUERIES_COMBINATIONS,
47+
...SYNC_QUERIES_COMBINATIONS,
48+
...ASYNC_QUERIES_COMBINATIONS,
4949
];
5050

5151
const ASYNC_UTILS = [
+50
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
'use strict';
2+
3+
const rule = require('../../../lib/rules/prefer-screen-queries');
4+
const { ALL_QUERIES_COMBINATIONS } = require('../../../lib/utils');
5+
const RuleTester = require('eslint').RuleTester;
6+
7+
// ------------------------------------------------------------------------------
8+
// Tests
9+
// ------------------------------------------------------------------------------
10+
11+
const ruleTester = new RuleTester({ parserOptions: { ecmaVersion: 2018 } });
12+
ruleTester.run('prefer-screen-queries', rule, {
13+
valid: [
14+
...ALL_QUERIES_COMBINATIONS.map(queryMethod => ({
15+
code: `screen.${queryMethod}()`,
16+
})),
17+
{
18+
code: `otherFunctionShouldNotThrow()`,
19+
},
20+
{
21+
code: `component.otherFunctionShouldNotThrow()`,
22+
},
23+
],
24+
25+
invalid: [
26+
...ALL_QUERIES_COMBINATIONS.map(queryMethod => ({
27+
code: `${queryMethod}()`,
28+
errors: [
29+
{
30+
messageId: 'preferScreenQueries',
31+
data: {
32+
name: queryMethod,
33+
},
34+
},
35+
],
36+
})),
37+
38+
...ALL_QUERIES_COMBINATIONS.map(queryMethod => ({
39+
code: `component.${queryMethod}()`,
40+
errors: [
41+
{
42+
messageId: 'preferScreenQueries',
43+
data: {
44+
name: queryMethod,
45+
},
46+
},
47+
],
48+
})),
49+
],
50+
});

0 commit comments

Comments
 (0)