diff --git a/lib/rules/prefer-screen-queries.ts b/lib/rules/prefer-screen-queries.ts index 9e717b6a..c37d40a9 100644 --- a/lib/rules/prefer-screen-queries.ts +++ b/lib/rules/prefer-screen-queries.ts @@ -11,6 +11,7 @@ import { isObjectPattern, isProperty, } from '../node-utils'; +import { resolveToTestingLibraryFn } from '../utils'; import type { TSESTree } from '@typescript-eslint/utils'; @@ -181,10 +182,11 @@ export default createTestingLibraryRule({ 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); } diff --git a/tests/lib/rules/prefer-screen-queries.test.ts b/tests/lib/rules/prefer-screen-queries.test.ts index b49ae2ad..a4a65302 100644 --- a/tests/lib/rules/prefer-screen-queries.test.ts +++ b/tests/lib/rules/prefer-screen-queries.test.ts @@ -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') @@ -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') @@ -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: ` @@ -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 })