Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions lib/rules/prefer-presence-queries.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,12 @@ export default createTestingLibraryRule<Options, MessageIds>({
return {
'CallExpression Identifier'(node: TSESTree.Identifier) {
const expectCallNode = findClosestCallNode(node, 'expect');
const withinCallNode = findClosestCallNode(node, 'within');

if (withinCallNode) {
//Ignore false-positives where our query is inside a within
return;
}

if (!expectCallNode || !isMemberExpression(expectCallNode.parent)) {
return;
Expand Down
3 changes: 3 additions & 0 deletions tests/lib/rules/prefer-presence-queries.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -837,6 +837,9 @@ ruleTester.run(RULE_NAME, rule, {
// right after clicking submit button it disappears
expect(submitButton).not.toBeInTheDocument()
`,
`// checking absence on getBy* inside a within with queryBy* outside the within
expect(within(screen.getByRole("button")).queryByText("Hello")).not.toBeInTheDocument()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you add another test to check that chained queries from within are reported as expected tho?

expect(within(screen.getByRole("button")).getByText("Hello")).not.toBeInTheDocument()

The example above should be reported, but I don't think it will be by your implementation.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good call. I think maybe I need to rethink the way I am handling this - it's not sufficient to say "ignore everything inside a within." Lemme rethink and re-PR.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually I think this does correctly handle this use case. I've added the unit test as requested and will re-raise the PR - it correctly deals with this exact case.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Belco90 reopened at #717, with the additional test case

`,
],
invalid: [
// cases: asserting absence incorrectly with `getBy*` queries
Expand Down