-
Notifications
You must be signed in to change notification settings - Fork 147
refactor(render-result-naming-convention): refine checks to decide if coming from Testing Library #282
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
refactor(render-result-naming-convention): refine checks to decide if coming from Testing Library #282
Changes from all commits
2a7ab42
97e8146
2617373
5e8d1a4
0a591b4
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,11 @@ | ||
import { createTestingLibraryRule } from '../create-testing-library-rule'; | ||
import { getDeepestIdentifierNode, isObjectPattern } from '../node-utils'; | ||
import { ASTUtils } from '@typescript-eslint/experimental-utils'; | ||
import { | ||
getDeepestIdentifierNode, | ||
getFunctionName, | ||
getInnermostReturningFunction, | ||
isObjectPattern, | ||
} from '../node-utils'; | ||
import { ASTUtils, TSESTree } from '@typescript-eslint/experimental-utils'; | ||
|
||
export const RULE_NAME = 'render-result-naming-convention'; | ||
export type MessageIds = 'renderResultNamingConvention'; | ||
|
@@ -30,15 +35,33 @@ export default createTestingLibraryRule<Options, MessageIds>({ | |
defaultOptions: [], | ||
|
||
create(context, _, helpers) { | ||
const renderWrapperNames: string[] = []; | ||
|
||
function detectRenderWrapper(node: TSESTree.Identifier): void { | ||
const innerFunction = getInnermostReturningFunction(context, node); | ||
|
||
if (innerFunction) { | ||
renderWrapperNames.push(getFunctionName(innerFunction)); | ||
} | ||
} | ||
|
||
return { | ||
'CallExpression Identifier'(node: TSESTree.Identifier) { | ||
if (helpers.isRenderUtil(node)) { | ||
detectRenderWrapper(node); | ||
} | ||
}, | ||
VariableDeclarator(node) { | ||
const initIdentifierNode = getDeepestIdentifierNode(node.init); | ||
|
||
if (!initIdentifierNode) { | ||
return; | ||
} | ||
|
||
if (!helpers.isRenderUtil(initIdentifierNode)) { | ||
if ( | ||
!helpers.isRenderUtil(initIdentifierNode) && | ||
!renderWrapperNames.includes(initIdentifierNode.name) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Detecting nested/wrapped |
||
) { | ||
return; | ||
} | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -126,6 +126,19 @@ ruleTester.run(RULE_NAME, rule, { | |
const utils = render() | ||
`, | ||
}, | ||
{ | ||
settings: { | ||
'testing-library/utils-module': 'test-utils', | ||
}, | ||
code: ` | ||
// case (render util): aggressive reporting disabled - method with same name | ||
// as TL method but not coming from TL module is valid | ||
import { render as testingLibraryRender } from 'test-utils' | ||
import { render } from 'somewhere-else' | ||
|
||
const utils = render() | ||
`, | ||
}, | ||
|
||
// Test Cases for presence/absence assertions | ||
// cases: asserts not related to presence/absence | ||
|
@@ -287,6 +300,21 @@ ruleTester.run(RULE_NAME, rule, { | |
waitFor() | ||
`, | ||
}, | ||
{ | ||
settings: { | ||
'testing-library/utils-module': 'test-utils', | ||
}, | ||
code: ` | ||
// case (async util): aggressive reporting disabled - method with same name | ||
// as TL method but not coming from TL module is valid | ||
import { waitFor as testingLibraryWaitFor } from 'test-utils' | ||
import { waitFor } from 'somewhere-else' | ||
|
||
test('this should not be reported', () => { | ||
waitFor() | ||
}); | ||
`, | ||
}, | ||
|
||
// Test Cases for all settings mixed | ||
{ | ||
|
@@ -642,26 +670,6 @@ ruleTester.run(RULE_NAME, rule, { | |
}, | ||
], | ||
}, | ||
{ | ||
settings: { | ||
'testing-library/utils-module': 'test-utils', | ||
}, | ||
code: ` | ||
// case: waitFor from object property shadowed name is checked correctly | ||
import * as tl from 'test-utils' | ||
const obj = { tl } | ||
|
||
obj.module.waitFor(() => {}) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't even know what I was trying to test here 😂 |
||
`, | ||
errors: [ | ||
{ | ||
line: 6, | ||
column: 20, | ||
messageId: 'asyncUtilError', | ||
data: { utilName: 'waitFor' }, | ||
}, | ||
], | ||
}, | ||
{ | ||
settings: { | ||
'testing-library/utils-module': 'test-utils', | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -218,6 +218,21 @@ ruleTester.run(RULE_NAME, rule, { | |
cy.wait(); | ||
`, | ||
}, | ||
{ | ||
settings: { | ||
'testing-library/utils-module': 'test-utils', | ||
}, | ||
code: ` | ||
// case: aggressive reporting disabled - method named same as invalid method | ||
// but not coming from Testing Library is valid | ||
import { wait as testingLibraryWait } from 'test-utils' | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Adding an extra test to this rule since was the only one using |
||
import { wait } from 'somewhere-else' | ||
|
||
async () => { | ||
await wait(); | ||
} | ||
`, | ||
}, | ||
{ | ||
// https://github.com/testing-library/eslint-plugin-testing-library/issues/145 | ||
code: `import * as foo from 'imNoTestingLibrary'; | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If any import found, it was assuming the given node was related to the import. But that's not always the case. In the example from the PR description "render" import was found but not the one renamed, so adding this extra check fixes it!