Skip to content

feat(no-debug): scan for screen.debug() #73

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Feb 1, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 14 additions & 1 deletion docs/rules/no-debug.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,23 @@ Examples of **incorrect** code for this rule:
```js
const { debug } = render(<Hello />);
debug();
// OR
```

```js
const utils = render(<Hello />);
utils.debug();
```

```js
import { screen } from '@testing-library/dom';
screen.debug();
```

```js
const { screen } = require('@testing-library/react');
screen.debug();
```

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.

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

- [debug API in React Testing Library](https://testing-library.com/docs/react-testing-library/api#debug)
- [`screen.debug` in Dom Testing Library](https://testing-library.com/docs/dom-testing-library/api-queries#screendebug)
60 changes: 60 additions & 0 deletions lib/rules/no-debug.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,14 @@

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

const LIBRARY_MODULES_WITH_SCREEN = [
'@testing-library/dom',
'@testing-library/react',
'@testing-library/preact',
'@testing-library/vue',
'@testing-library/svelte',
];

module.exports = {
meta: {
type: 'problem',
Expand Down Expand Up @@ -36,6 +44,8 @@ module.exports = {
[{ renderFunctions }] = context.options;
}

let hasImportedScreen = false;

return {
VariableDeclarator(node) {
if (
Expand All @@ -57,6 +67,44 @@ module.exports = {
}
}
},
[`VariableDeclarator > CallExpression > Identifier[name="require"]`](
node
) {
const { arguments: args } = node.parent;

const literalNodeScreenModuleName = args.find(args =>
LIBRARY_MODULES_WITH_SCREEN.includes(args.value)
);

if (!literalNodeScreenModuleName) {
return;
}

const declaratorNode = node.parent.parent;

if (
declaratorNode.id.type === 'ObjectPattern' &&
declaratorNode.id.properties.some(
property => property.key.name === 'screen'
)
) {
hasImportedScreen = true;
}
},
ImportDeclaration(node) {
const screenModuleName = LIBRARY_MODULES_WITH_SCREEN.find(
module => module === node.source.value
);

if (
screenModuleName &&
node.specifiers.some(
specifier => specifier.imported.name === 'screen'
)
) {
hasImportedScreen = true;
}
},
[`CallExpression > Identifier[name="debug"]`](node) {
if (hasDestructuredDebugStatement) {
context.report({
Expand All @@ -65,6 +113,18 @@ module.exports = {
});
}
},
[`CallExpression > MemberExpression > Identifier[name="debug"]`](node) {
if (
hasImportedScreen &&
node.parent &&
node.parent.object.name === 'screen'
) {
context.report({
node,
messageId: 'noDebug',
});
}
},
'Program:exit'() {
renderVariableDeclarators.forEach(renderVar => {
const renderVarReferences = context
Expand Down
56 changes: 56 additions & 0 deletions tests/lib/rules/no-debug.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ const ruleTester = new RuleTester({
ecmaFeatures: {
jsx: true,
},
sourceType: 'module',
},
});
ruleTester.run('no-debug', rule, {
Expand Down Expand Up @@ -56,6 +57,39 @@ ruleTester.run('no-debug', rule, {
utils.foo()
`,
},
{
code: `screen.debug()`,
},
{
code: `
const { screen } = require('@testing-library/dom')
screen.debug
`,
},
{
code: `
import { screen } from '@testing-library/dom'
screen.debug
`,
},
{
code: `const { queries } = require('@testing-library/dom')`,
},
{
code: `import { queries } from '@testing-library/dom'`,
},
{
code: `
const { screen } = require('something-else')
screen.debug()
`,
},
{
code: `
import { screen } from 'something-else'
screen.debug()
`,
},
],

invalid: [
Expand Down Expand Up @@ -113,5 +147,27 @@ ruleTester.run('no-debug', rule, {
},
],
},
{
code: `
const { screen } = require('@testing-library/dom')
screen.debug()
`,
errors: [
{
messageId: 'noDebug',
},
],
},
{
code: `
import { screen } from '@testing-library/dom'
screen.debug()
`,
errors: [
{
messageId: 'noDebug',
},
],
},
],
});