File tree 6 files changed +124
-2
lines changed
6 files changed +124
-2
lines changed Original file line number Diff line number Diff line change @@ -147,6 +147,7 @@ To enable this configuration use the `extends` property in your
147
147
| [ no-manual-cleanup] ( docs/rules/no-manual-cleanup.md ) | Disallow the use of ` cleanup ` | | |
148
148
| [ no-wait-for-empty-callback] ( docs/rules/no-wait-for-empty-callback.md ) | Disallow empty callbacks for ` waitFor ` and ` waitForElementToBeRemoved ` | | |
149
149
| [ 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 | | |
150
151
| [ prefer-wait-for] ( docs/rules/prefer-wait-for.md ) | Use ` waitFor ` instead of deprecated wait methods | | ![ fixable-badge] [ ] |
151
152
152
153
[ build-badge ] : https://img.shields.io/travis/testing-library/eslint-plugin-testing-library?style=flat-square
Original file line number Diff line number Diff line change
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 )
Original file line number Diff line number Diff line change @@ -12,6 +12,7 @@ const rules = {
12
12
'no-manual-cleanup' : require ( './rules/no-manual-cleanup' ) ,
13
13
'no-wait-for-empty-callback' : require ( './rules/no-wait-for-empty-callback' ) ,
14
14
'prefer-explicit-assert' : require ( './rules/prefer-explicit-assert' ) ,
15
+ 'prefer-screen-queries' : require ( './rules/prefer-screen-queries' ) ,
15
16
'prefer-wait-for' : require ( './rules/prefer-wait-for' ) ,
16
17
} ;
17
18
Original file line number Diff line number Diff line change
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
+ } ;
Original file line number Diff line number Diff line change @@ -44,8 +44,8 @@ const ASYNC_QUERIES_COMBINATIONS = combineQueries(
44
44
) ;
45
45
46
46
const ALL_QUERIES_COMBINATIONS = [
47
- SYNC_QUERIES_COMBINATIONS ,
48
- ASYNC_QUERIES_COMBINATIONS ,
47
+ ... SYNC_QUERIES_COMBINATIONS ,
48
+ ... ASYNC_QUERIES_COMBINATIONS ,
49
49
] ;
50
50
51
51
const ASYNC_UTILS = [
Original file line number Diff line number Diff line change
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
+ } ) ;
You can’t perform that action at this time.
0 commit comments