Skip to content

Commit 153bb3a

Browse files
authored
feat(no-debug): scan for screen.debug() (#73)
* feat(no-debug): scan for `screen.debug()` * test(no-debug): improve coverage after ec38b92 * docs(no-debug): add `screen.debug` example * docs(no-debug): add link to `screen.debug` docs * docs(no-debug): remove unnecessary note
1 parent 050cd4f commit 153bb3a

File tree

3 files changed

+130
-1
lines changed

3 files changed

+130
-1
lines changed

docs/rules/no-debug.md

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,23 @@ Examples of **incorrect** code for this rule:
1111
```js
1212
const { debug } = render(<Hello />);
1313
debug();
14-
// OR
14+
```
15+
16+
```js
1517
const utils = render(<Hello />);
1618
utils.debug();
1719
```
1820

21+
```js
22+
import { screen } from '@testing-library/dom';
23+
screen.debug();
24+
```
25+
26+
```js
27+
const { screen } = require('@testing-library/react');
28+
screen.debug();
29+
```
30+
1931
If you use [custom render functions](https://testing-library.com/docs/example-react-redux) then you can set a config option in your `.eslintrc` to look for these.
2032

2133
```
@@ -25,3 +37,4 @@ If you use [custom render functions](https://testing-library.com/docs/example-re
2537
## Further Reading
2638

2739
- [debug API in React Testing Library](https://testing-library.com/docs/react-testing-library/api#debug)
40+
- [`screen.debug` in Dom Testing Library](https://testing-library.com/docs/dom-testing-library/api-queries#screendebug)

lib/rules/no-debug.js

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,14 @@
22

33
const { getDocsUrl } = require('../utils');
44

5+
const LIBRARY_MODULES_WITH_SCREEN = [
6+
'@testing-library/dom',
7+
'@testing-library/react',
8+
'@testing-library/preact',
9+
'@testing-library/vue',
10+
'@testing-library/svelte',
11+
];
12+
513
module.exports = {
614
meta: {
715
type: 'problem',
@@ -36,6 +44,8 @@ module.exports = {
3644
[{ renderFunctions }] = context.options;
3745
}
3846

47+
let hasImportedScreen = false;
48+
3949
return {
4050
VariableDeclarator(node) {
4151
if (
@@ -57,6 +67,44 @@ module.exports = {
5767
}
5868
}
5969
},
70+
[`VariableDeclarator > CallExpression > Identifier[name="require"]`](
71+
node
72+
) {
73+
const { arguments: args } = node.parent;
74+
75+
const literalNodeScreenModuleName = args.find(args =>
76+
LIBRARY_MODULES_WITH_SCREEN.includes(args.value)
77+
);
78+
79+
if (!literalNodeScreenModuleName) {
80+
return;
81+
}
82+
83+
const declaratorNode = node.parent.parent;
84+
85+
if (
86+
declaratorNode.id.type === 'ObjectPattern' &&
87+
declaratorNode.id.properties.some(
88+
property => property.key.name === 'screen'
89+
)
90+
) {
91+
hasImportedScreen = true;
92+
}
93+
},
94+
ImportDeclaration(node) {
95+
const screenModuleName = LIBRARY_MODULES_WITH_SCREEN.find(
96+
module => module === node.source.value
97+
);
98+
99+
if (
100+
screenModuleName &&
101+
node.specifiers.some(
102+
specifier => specifier.imported.name === 'screen'
103+
)
104+
) {
105+
hasImportedScreen = true;
106+
}
107+
},
60108
[`CallExpression > Identifier[name="debug"]`](node) {
61109
if (hasDestructuredDebugStatement) {
62110
context.report({
@@ -65,6 +113,18 @@ module.exports = {
65113
});
66114
}
67115
},
116+
[`CallExpression > MemberExpression > Identifier[name="debug"]`](node) {
117+
if (
118+
hasImportedScreen &&
119+
node.parent &&
120+
node.parent.object.name === 'screen'
121+
) {
122+
context.report({
123+
node,
124+
messageId: 'noDebug',
125+
});
126+
}
127+
},
68128
'Program:exit'() {
69129
renderVariableDeclarators.forEach(renderVar => {
70130
const renderVarReferences = context

tests/lib/rules/no-debug.js

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ const ruleTester = new RuleTester({
1717
ecmaFeatures: {
1818
jsx: true,
1919
},
20+
sourceType: 'module',
2021
},
2122
});
2223
ruleTester.run('no-debug', rule, {
@@ -56,6 +57,39 @@ ruleTester.run('no-debug', rule, {
5657
utils.foo()
5758
`,
5859
},
60+
{
61+
code: `screen.debug()`,
62+
},
63+
{
64+
code: `
65+
const { screen } = require('@testing-library/dom')
66+
screen.debug
67+
`,
68+
},
69+
{
70+
code: `
71+
import { screen } from '@testing-library/dom'
72+
screen.debug
73+
`,
74+
},
75+
{
76+
code: `const { queries } = require('@testing-library/dom')`,
77+
},
78+
{
79+
code: `import { queries } from '@testing-library/dom'`,
80+
},
81+
{
82+
code: `
83+
const { screen } = require('something-else')
84+
screen.debug()
85+
`,
86+
},
87+
{
88+
code: `
89+
import { screen } from 'something-else'
90+
screen.debug()
91+
`,
92+
},
5993
],
6094

6195
invalid: [
@@ -113,5 +147,27 @@ ruleTester.run('no-debug', rule, {
113147
},
114148
],
115149
},
150+
{
151+
code: `
152+
const { screen } = require('@testing-library/dom')
153+
screen.debug()
154+
`,
155+
errors: [
156+
{
157+
messageId: 'noDebug',
158+
},
159+
],
160+
},
161+
{
162+
code: `
163+
import { screen } from '@testing-library/dom'
164+
screen.debug()
165+
`,
166+
errors: [
167+
{
168+
messageId: 'noDebug',
169+
},
170+
],
171+
},
116172
],
117173
});

0 commit comments

Comments
 (0)