Skip to content
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
11 changes: 11 additions & 0 deletions docs/rules/no-node-access.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,17 @@ const signinModal = getByLabelText('Sign In');
within(signinModal).getByPlaceholderText('Username');
```

```js
import { screen } from '@testing-library/react';

function ComponentA(props) {
// props.children is not reported
return <div>{props.children}</div>;
}

render(<ComponentA />);
```

```js
// If is not importing a testing-library package

Expand Down
7 changes: 7 additions & 0 deletions lib/rules/no-node-access.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,13 @@ export default createTestingLibraryRule<Options, MessageIds>({
return;
}

if (
ASTUtils.isIdentifier(node.object) &&
node.object.name === 'props'
) {
return;
}

context.report({
node,
loc: node.property.loc.start,
Expand Down
35 changes: 35 additions & 0 deletions tests/lib/rules/no-node-access.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,41 @@ ruleTester.run(RULE_NAME, rule, {
);
`,
},
{
code: `// issue #386 examples, props.children should not be reported
import { screen } from '${testingFramework}';
jest.mock('@/some/path', () => ({
someProperty: jest.fn((props) => props.children),
}));
`,
},
{
code: `// issue #386 examples
import { screen } from '${testingFramework}';
function ComponentA(props) {
if (props.children) {
// ...
}

return <div>{props.children}</div>
}
`,
},
{
code: `/* related to issue #386 fix
* now all node accessing properties (listed in lib/utils/index.ts, in PROPERTIES_RETURNING_NODES)
* will not be reported by this rule because anything props.something won't be reported.
*/
import { screen } from '${testingFramework}';
function ComponentA(props) {
if (props.firstChild) {
// ...
}

return <div>{props.nextSibling}</div>
}
`,
},
{
settings: {
'testing-library/utils-module': 'test-utils',
Expand Down