Skip to content

Commit

Permalink
feat(no-debug): option for render function names (#19)
Browse files Browse the repository at this point in the history
* feat(no-debug): render function names

* feat(no-debug): added docs
  • Loading branch information
benmonro authored and Belco90 committed Oct 9, 2019
1 parent 63c4fde commit 648c7b4
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 2 deletions.
6 changes: 6 additions & 0 deletions docs/rules/no-debug.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,12 @@ const utils = render(<Hello />);
utils.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.

```
"testing-library/no-debug": ["error", {"renderFunctions":["renderWithRedux", "renderWithRouter"]}],
```

## Further Reading

- [debug API in React Testing Library](https://testing-library.com/docs/react-testing-library/api#debug)
21 changes: 19 additions & 2 deletions lib/rules/no-debug.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,35 @@ module.exports = {
noDebug: 'Unexpected debug statement',
},
fixable: null,
schema: [],
schema: [
{
type: 'object',
properties: {
renderFunctions: {
type: 'array',
},
},
},
],
},

create: function(context) {
let hasDestructuredDebugStatement = false;
const renderVariableDeclarators = [];

let renderFunctions = [];
if (context.options && context.options.length > 0) {
[{ renderFunctions }] = context.options;
}

return {
VariableDeclarator(node) {
if (
node.init &&
node.init.callee &&
node.init.callee.name === 'render'
['render', ...renderFunctions].some(
name => name === node.init.callee.name
)
) {
if (
node.id.type === 'ObjectPattern' &&
Expand Down
16 changes: 16 additions & 0 deletions tests/lib/rules/no-debug.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,22 @@ ruleTester.run('no-debug', rule, {
},
],
},
{
code: `
const { debug } = renderWithRedux(<Component/>)
debug()
`,
options: [
{
renderFunctions: ['renderWithRedux'],
},
],
errors: [
{
messageId: 'noDebug',
},
],
},
{
code: `
const utils = render(<Component/>)
Expand Down

0 comments on commit 648c7b4

Please sign in to comment.