Skip to content

Commit d60b762

Browse files
feat: add prefer-screen-queries rule (#99)
* feat: add prefer-screen-queries Closes #95 * docs: review changes
1 parent 4e8003d commit d60b762

File tree

6 files changed

+124
-2
lines changed

6 files changed

+124
-2
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,7 @@ To enable this configuration use the `extends` property in your
147147
| [no-manual-cleanup](docs/rules/no-manual-cleanup.md) | Disallow the use of `cleanup` | | |
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 | | |
150+
| [prefer-screen-queries](docs/rules/prefer-screen-queries.md) | Suggest using screen while using queries | | |
150151
| [prefer-wait-for](docs/rules/prefer-wait-for.md) | Use `waitFor` instead of deprecated wait methods | | ![fixable-badge][] |
151152

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

docs/rules/prefer-screen-queries.md

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# Suggest using screen while using queries (prefer-screen-queries)
2+
3+
## Rule Details
4+
5+
DOM Testing Library (and other Testing Library frameworks built on top of it) exports a `screen` object which has every query (and a `debug` method). This works better with autocomplete and makes each test a little simpler to write and maintain.
6+
This rule aims to force writing tests using queries directly from `screen` object rather than destructuring them from `render` result.
7+
8+
Examples of **incorrect** code for this rule:
9+
10+
```js
11+
// calling a query from the `render` method
12+
const { getByText } = render(<Component />);
13+
getByText('foo');
14+
15+
const utils = render(<Component />);
16+
utils.getByText('foo');
17+
```
18+
19+
Examples of **correct** code for this rule:
20+
21+
```js
22+
import { screen } from '@testing-library/any-framework';
23+
24+
render(<Component />);
25+
screen.getByText('foo');
26+
```
27+
28+
## Further Reading
29+
30+
- [`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
@@ -12,6 +12,7 @@ const rules = {
1212
'no-manual-cleanup': require('./rules/no-manual-cleanup'),
1313
'no-wait-for-empty-callback': require('./rules/no-wait-for-empty-callback'),
1414
'prefer-explicit-assert': require('./rules/prefer-explicit-assert'),
15+
'prefer-screen-queries': require('./rules/prefer-screen-queries'),
1516
'prefer-wait-for': require('./rules/prefer-wait-for'),
1617
};
1718

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)