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
6 changes: 4 additions & 2 deletions lib/rules/prefer-screen-queries.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {
isObjectPattern,
isProperty,
} from '../node-utils';
import { resolveToTestingLibraryFn } from '../utils';

import type { TSESTree } from '@typescript-eslint/utils';

Expand Down Expand Up @@ -181,10 +182,11 @@ export default createTestingLibraryRule<Options, MessageIds>({
reportInvalidUsage(identifierNode);
return;
}

const testingLibraryFn = resolveToTestingLibraryFn(node, context);
if (
ASTUtils.isIdentifier(memberExpressionNode.object) &&
!isIdentifierAllowed(memberExpressionNode.object.name)
!isIdentifierAllowed(memberExpressionNode.object.name) &&
!isIdentifierAllowed(testingLibraryFn?.original ?? '')
) {
reportInvalidUsage(identifierNode);
}
Expand Down
25 changes: 22 additions & 3 deletions tests/lib/rules/prefer-screen-queries.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ ruleTester.run(RULE_NAME, rule, {
code: `
import { render } from '${testingFramework}'
import { ${query} } from 'custom-queries'

test("imported custom queries, since they can't be used through screen", () => {
render(foo)
${query}('bar')
Expand All @@ -67,7 +67,7 @@ ruleTester.run(RULE_NAME, rule, {
...CUSTOM_QUERY_COMBINATIONS.map((query) => ({
code: `
import { render } from '${testingFramework}'

test("render-returned custom queries, since they can't be used through screen", () => {
const { ${query} } = render(foo)
${query}('bar')
Expand All @@ -80,13 +80,22 @@ ruleTester.run(RULE_NAME, rule, {
},
code: `
import { render } from '${testingFramework}'

test("custom queries + custom-queries setting, since they can't be used through screen", () => {
const { ${query} } = render(foo)
${query}('bar')
})
`,
})),
{
code: `
import { screen as rtlScreen } from '${testingFramework}'

test("test", () => {
rtlScreen.getByText('hoge');
})
`,
},
]),
{
code: `
Expand Down Expand Up @@ -136,6 +145,16 @@ ruleTester.run(RULE_NAME, rule, {
utils.unmount();
`,
},
{
settings: { 'testing-library/utils-module': 'test-utils' },
code: `
import { screen as rtlScreen } from 'test-utils'

test("test", () => {
rtlScreen.getByText('hoge');
})
`,
},
...ALL_QUERIES_COMBINATIONS.map((queryMethod: string) => ({
code: `
const { ${queryMethod} } = render(baz, { baseElement: treeA })
Expand Down