-
Notifications
You must be signed in to change notification settings - Fork 142
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add render-result-naming-convention rule (#200)
* feat: rule skeleton * test: first round * feat: rule implementation round 1 * refactor: move hasTestingLibraryImportModule * test: fix invalid lines * feat: check imported module * feat: check imported render renamed * feat: check custom render * test: add more valid tests for custom render functions * test: update tests for render wrapper functions * docs: add rule docs * test: increase coverage up to 100% * fix: add rule meta description * docs: update rule details to mention screen object * refactor: return as soon as conditions are not met * feat: check wildcard imports * refactor: rename default import * docs: include render result link
- Loading branch information
Showing
7 changed files
with
602 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
# Enforce a valid naming for return value from `render` (render-result-naming-convention) | ||
|
||
> The name `wrapper` is old cruft from `enzyme` and we don't need that here. The return value from `render` is not "wrapping" anything. It's simply a collection of utilities that you should actually not often need anyway. | ||
## Rule Details | ||
|
||
This rule aims to ensure the return value from `render` is named properly. | ||
|
||
Ideally, you should destructure the minimum utils that you need from `render`, combined with using queries from [`screen` object](https://github.com/testing-library/eslint-plugin-testing-library/blob/master/docs/rules/prefer-screen-queries.md). In case you need to save the collection of utils returned in a variable, its name should be either `view` or `utils`, as `render` is not wrapping anything: it's just returning a collection of utilities. Every other name for that variable will be considered invalid. | ||
|
||
To sum up these rules, the allowed naming convention for return value from `render` is: | ||
|
||
- destructuring | ||
- `view` | ||
- `utils` | ||
|
||
Examples of **incorrect** code for this rule: | ||
|
||
```javascript | ||
import { render } from '@testing-library/framework'; | ||
|
||
// ... | ||
|
||
// return value from `render` shouldn't be kept in a var called "wrapper" | ||
const wrapper = render(<SomeComponent />); | ||
``` | ||
|
||
```javascript | ||
import { render } from '@testing-library/framework'; | ||
|
||
// ... | ||
|
||
// return value from `render` shouldn't be kept in a var called "component" | ||
const component = render(<SomeComponent />); | ||
``` | ||
|
||
```javascript | ||
import { render } from '@testing-library/framework'; | ||
|
||
// ... | ||
|
||
// to sum up: return value from `render` shouldn't be kept in a var called other than "view" or "utils" | ||
const somethingElse = render(<SomeComponent />); | ||
``` | ||
|
||
Examples of **correct** code for this rule: | ||
|
||
```javascript | ||
import { render } from '@testing-library/framework'; | ||
|
||
// ... | ||
|
||
// destructuring return value from `render` is correct | ||
const { unmount, rerender } = render(<SomeComponent />); | ||
``` | ||
|
||
```javascript | ||
import { render } from '@testing-library/framework'; | ||
|
||
// ... | ||
|
||
// keeping return value from `render` in a var called "view" is correct | ||
const view = render(<SomeComponent />); | ||
``` | ||
|
||
```javascript | ||
import { render } from '@testing-library/framework'; | ||
|
||
// ... | ||
|
||
// keeping return value from `render` in a var called "utils" is correct | ||
const utils = render(<SomeComponent />); | ||
``` | ||
|
||
## Further Reading | ||
|
||
- [Common Mistakes with React Testing Library](https://kentcdodds.com/blog/common-mistakes-with-react-testing-library#using-wrapper-as-the-variable-name-for-the-return-value-from-render) | ||
- [`render` Result](https://testing-library.com/docs/react-testing-library/api#render-result) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,145 @@ | ||
import { ESLintUtils, TSESTree } from '@typescript-eslint/experimental-utils'; | ||
import { getDocsUrl, hasTestingLibraryImportModule } from '../utils'; | ||
import { | ||
isCallExpression, | ||
isIdentifier, | ||
isImportSpecifier, | ||
isMemberExpression, | ||
isObjectPattern, | ||
isRenderVariableDeclarator, | ||
} from '../node-utils'; | ||
|
||
export const RULE_NAME = 'render-result-naming-convention'; | ||
|
||
const ALLOWED_VAR_NAMES = ['view', 'utils']; | ||
const ALLOWED_VAR_NAMES_TEXT = ALLOWED_VAR_NAMES.map( | ||
name => '`' + name + '`' | ||
).join(', '); | ||
|
||
export default ESLintUtils.RuleCreator(getDocsUrl)({ | ||
name: RULE_NAME, | ||
meta: { | ||
type: 'suggestion', | ||
docs: { | ||
description: 'Enforce a valid naming for return value from `render`', | ||
category: 'Best Practices', | ||
recommended: false, | ||
}, | ||
messages: { | ||
invalidRenderResultName: `\`{{ varName }}\` is not a recommended name for \`render\` returned value. Instead, you should destructure it, or call it using one of the valid choices: ${ALLOWED_VAR_NAMES_TEXT}`, | ||
}, | ||
fixable: null, | ||
schema: [ | ||
{ | ||
type: 'object', | ||
properties: { | ||
renderFunctions: { | ||
type: 'array', | ||
}, | ||
}, | ||
}, | ||
], | ||
}, | ||
defaultOptions: [ | ||
{ | ||
renderFunctions: [], | ||
}, | ||
], | ||
|
||
create(context, [options]) { | ||
const { renderFunctions } = options; | ||
let renderAlias: string | undefined; | ||
let wildcardImportName: string | undefined; | ||
|
||
return { | ||
// check named imports | ||
ImportDeclaration(node: TSESTree.ImportDeclaration) { | ||
if (!hasTestingLibraryImportModule(node)) { | ||
return; | ||
} | ||
const renderImport = node.specifiers.find( | ||
node => isImportSpecifier(node) && node.imported.name === 'render' | ||
); | ||
|
||
if (!renderImport) { | ||
return; | ||
} | ||
|
||
renderAlias = renderImport.local.name; | ||
}, | ||
// check wildcard imports | ||
'ImportDeclaration ImportNamespaceSpecifier'( | ||
node: TSESTree.ImportNamespaceSpecifier | ||
) { | ||
if ( | ||
!hasTestingLibraryImportModule( | ||
node.parent as TSESTree.ImportDeclaration | ||
) | ||
) { | ||
return; | ||
} | ||
|
||
wildcardImportName = node.local.name; | ||
}, | ||
VariableDeclarator(node: TSESTree.VariableDeclarator) { | ||
// check if destructuring return value from render | ||
if (isObjectPattern(node.id)) { | ||
return; | ||
} | ||
|
||
const isValidRenderDeclarator = isRenderVariableDeclarator(node, [ | ||
...renderFunctions, | ||
renderAlias, | ||
]); | ||
const isValidWildcardImport = !!wildcardImportName; | ||
|
||
// check if is a Testing Library related import | ||
if (!isValidRenderDeclarator && !isValidWildcardImport) { | ||
return; | ||
} | ||
|
||
const renderFunctionName = | ||
isCallExpression(node.init) && | ||
isIdentifier(node.init.callee) && | ||
node.init.callee.name; | ||
|
||
const renderFunctionObjectName = | ||
isCallExpression(node.init) && | ||
isMemberExpression(node.init.callee) && | ||
isIdentifier(node.init.callee.property) && | ||
isIdentifier(node.init.callee.object) && | ||
node.init.callee.property.name === 'render' && | ||
node.init.callee.object.name; | ||
|
||
const isRenderAlias = !!renderAlias; | ||
const isCustomRender = renderFunctions.includes(renderFunctionName); | ||
const isWildCardRender = | ||
renderFunctionObjectName && | ||
renderFunctionObjectName === wildcardImportName; | ||
|
||
// check if is a qualified render function | ||
if (!isRenderAlias && !isCustomRender && !isWildCardRender) { | ||
return; | ||
} | ||
|
||
const renderResultName = isIdentifier(node.id) && node.id.name; | ||
const isAllowedRenderResultName = ALLOWED_VAR_NAMES.includes( | ||
renderResultName | ||
); | ||
|
||
// check if return value var name is allowed | ||
if (isAllowedRenderResultName) { | ||
return; | ||
} | ||
|
||
context.report({ | ||
node, | ||
messageId: 'invalidRenderResultName', | ||
data: { | ||
varName: renderResultName, | ||
}, | ||
}); | ||
}, | ||
}; | ||
}, | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.