- 
                Notifications
    
You must be signed in to change notification settings  - Fork 153
 
feat(no-render-in-setup): adds no-render-in-setup rule, closes #207 #209
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
          
     Merged
      
      
            Belco90
  merged 6 commits into
  testing-library:master
from
alessbell:pr/no-render-in-setup-rule
  
      
      
   
  Aug 10, 2020 
      
    
  
     Merged
                    Changes from 4 commits
      Commits
    
    
            Show all changes
          
          
            6 commits
          
        
        Select commit
          Hold shift + click to select a range
      
      3fb5364
              
                feat(no-render-in-setup): adds no-render-in-setup rule, closes #207
              
              
                alessbell 966f521
              
                refactor: address review comments
              
              
                alessbell ef1d4d9
              
                Update docs/rules/no-render-in-setup.md
              
              
                alessbell 2e469ba
              
                fix: updates docs, adds tests
              
              
                alessbell 0813194
              
                fix: handle require, add tests
              
              
                alessbell 542f094
              
                fix: code coverage
              
              
                alessbell 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
There are no files selected for viewing
  
    
      This file contains hidden or 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 hidden or 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,57 @@ | ||
| # Disallow the use of `render` in setup functions (no-render-in-setup) | ||
| 
     | 
||
| ## Rule Details | ||
| 
     | 
||
| This rule disallows the usage of `render` (or a custom render function) in setup functions (`beforeEach` and `beforeAll`) in favor of moving `render` closer to test assertions. | ||
| 
     | 
||
| Examples of **incorrect** code for this rule: | ||
| 
     | 
||
| ```js | ||
| beforeEach(() => { | ||
| render(<MyComponent />); | ||
| }); | ||
| 
     | 
||
| it('Should have foo', () => { | ||
| expect(screen.getByText('foo')).toBeInTheDocument(); | ||
| }); | ||
| 
     | 
||
| it('Should have bar', () => { | ||
| expect(screen.getByText('bar')).toBeInTheDocument(); | ||
| }); | ||
| ``` | ||
| 
     | 
||
| ```js | ||
| beforeAll(() => { | ||
| render(<MyComponent />); | ||
| }); | ||
| 
     | 
||
| it('Should have foo', () => { | ||
| expect(screen.getByText('foo')).toBeInTheDocument(); | ||
| }); | ||
| 
     | 
||
| it('Should have bar', () => { | ||
| expect(screen.getByText('bar')).toBeInTheDocument(); | ||
| }); | ||
| ``` | ||
| 
     | 
||
| Examples of **correct** code for this rule: | ||
| 
     | 
||
| ```js | ||
| it('Should have foo and bar', () => { | ||
| render(<MyComponent />); | ||
| expect(screen.getByText('foo')).toBeInTheDocument(); | ||
| expect(screen.getByText('bar')).toBeInTheDocument(); | ||
| }); | ||
| ``` | ||
| 
     | 
||
| If you use [custom render functions](https://testing-library.com/docs/example-react-redux) then you can set a config option in your `.eslintrc` to look for these. | ||
| 
     | 
||
| ``` | ||
| "testing-library/no-render-in-setup": ["error", {"renderFunctions": ["renderWithRedux", "renderWithRouter"]}], | ||
| ``` | ||
| 
     | 
||
| If you would like to allow the use of `render` (or a custom render function) in _either_ `beforeAll` or `beforeEach`, this can be configured using the option `allowTestingFrameworkSetupHook`. This may be useful if you have configured your tests to [skip auto cleanup](https://testing-library.com/docs/react-testing-library/setup#skipping-auto-cleanup). `allowTestingFrameworkSetupHook` is an enum that accepts either `"beforeAll"` or `"beforeEach"`. | ||
| 
     | 
||
| ``` | ||
| "testing-library/no-render-in-setup": ["error", {"allowTestingFrameworkSetupHook": "beforeAll"}], | ||
| ``` | 
  
    
      This file contains hidden or 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 hidden or 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 | 
|---|---|---|
| 
          
            
          
           | 
    @@ -50,6 +50,22 @@ export function isVariableDeclarator( | |
| return node && node.type === AST_NODE_TYPES.VariableDeclarator; | ||
| } | ||
| 
     | 
||
| export function isRenderFunction( | ||
| 
         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 thought this one was already moved to   | 
||
| callNode: TSESTree.CallExpression, | ||
| renderFunctions: string[] | ||
| ) { | ||
| // returns true for `render` and e.g. `customRenderFn` | ||
| // as well as `someLib.render` and `someUtils.customRenderFn` | ||
| return ['render', ...renderFunctions].some(name => { | ||
| return ( | ||
| (isIdentifier(callNode.callee) && name === callNode.callee.name) || | ||
| (isMemberExpression(callNode.callee) && | ||
| isIdentifier(callNode.callee.property) && | ||
| name === callNode.callee.property.name) | ||
| ); | ||
| }); | ||
| } | ||
| 
     | 
||
| export function isObjectPattern( | ||
| node: TSESTree.Node | ||
| ): node is TSESTree.ObjectPattern { | ||
| 
          
            
          
           | 
    ||
  
    
      This file contains hidden or 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 hidden or 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 hidden or 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,96 @@ | ||
| import { ESLintUtils, TSESTree } from '@typescript-eslint/experimental-utils'; | ||
| import { getDocsUrl, TESTING_FRAMEWORK_SETUP_HOOKS } from '../utils'; | ||
| import { | ||
| isIdentifier, | ||
| isCallExpression, | ||
| isRenderFunction, | ||
| } from '../node-utils'; | ||
| 
     | 
||
| export const RULE_NAME = 'no-render-in-setup'; | ||
| export type MessageIds = 'noRenderInSetup'; | ||
| 
     | 
||
| export function findClosestBeforeHook( | ||
| node: TSESTree.Node, | ||
| testingFrameworkSetupHooksToFilter: string[] | ||
| ): TSESTree.Identifier | null { | ||
| if (node === null) return null; | ||
| if ( | ||
| isCallExpression(node) && | ||
| isIdentifier(node.callee) && | ||
| testingFrameworkSetupHooksToFilter.includes(node.callee.name) | ||
| ) { | ||
| return node.callee; | ||
| } | ||
| 
     | 
||
| return findClosestBeforeHook(node.parent, testingFrameworkSetupHooksToFilter); | ||
| } | ||
| 
     | 
||
| export default ESLintUtils.RuleCreator(getDocsUrl)({ | ||
| name: RULE_NAME, | ||
| meta: { | ||
| type: 'problem', | ||
| docs: { | ||
| description: 'Disallow the use of `render` in setup functions', | ||
| category: 'Best Practices', | ||
| recommended: false, | ||
| }, | ||
| messages: { | ||
| noRenderInSetup: | ||
| 'Move `render` out of `{{name}}` and into individual tests.', | ||
| }, | ||
| fixable: null, | ||
| schema: [ | ||
| { | ||
| type: 'object', | ||
| properties: { | ||
| renderFunctions: { | ||
| type: 'array', | ||
| }, | ||
| allowTestingFrameworkSetupHook: { | ||
| enum: TESTING_FRAMEWORK_SETUP_HOOKS, | ||
| }, | ||
| }, | ||
| anyOf: [ | ||
| { | ||
| required: ['renderFunctions'], | ||
| }, | ||
| { | ||
| required: ['allowTestingFrameworkSetupHook'], | ||
| }, | ||
| ], | ||
| }, | ||
| ], | ||
| }, | ||
| defaultOptions: [ | ||
| { | ||
| renderFunctions: [], | ||
| allowTestingFrameworkSetupHook: '', | ||
| }, | ||
| ], | ||
| 
     | 
||
| create(context, [{ renderFunctions, allowTestingFrameworkSetupHook }]) { | ||
| return { | ||
| CallExpression(node) { | ||
| let testingFrameworkSetupHooksToFilter = TESTING_FRAMEWORK_SETUP_HOOKS; | ||
| if (allowTestingFrameworkSetupHook.length !== 0) { | ||
| testingFrameworkSetupHooksToFilter = TESTING_FRAMEWORK_SETUP_HOOKS.filter( | ||
| hook => hook !== allowTestingFrameworkSetupHook | ||
| ); | ||
| } | ||
| const beforeHook = findClosestBeforeHook( | ||
| node, | ||
| testingFrameworkSetupHooksToFilter | ||
| ); | ||
| if (isRenderFunction(node, renderFunctions) && beforeHook) { | ||
| context.report({ | ||
| node, | ||
| messageId: 'noRenderInSetup', | ||
| data: { | ||
| name: beforeHook.name, | ||
                
      
                  alessbell marked this conversation as resolved.
               
          
            Show resolved
            Hide resolved
         | 
||
| }, | ||
| }); | ||
| } | ||
| }, | ||
| }; | ||
| }, | ||
| }); | ||
  
    
      This file contains hidden or 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.
        
    
  
  Add this suggestion to a batch that can be applied as a single commit.
  This suggestion is invalid because no changes were made to the code.
  Suggestions cannot be applied while the pull request is closed.
  Suggestions cannot be applied while viewing a subset of changes.
  Only one suggestion per line can be applied in a batch.
  Add this suggestion to a batch that can be applied as a single commit.
  Applying suggestions on deleted lines is not supported.
  You must change the existing code in this line in order to create a valid suggestion.
  Outdated suggestions cannot be applied.
  This suggestion has been applied or marked resolved.
  Suggestions cannot be applied from pending reviews.
  Suggestions cannot be applied on multi-line comments.
  Suggestions cannot be applied while the pull request is queued to merge.
  Suggestion cannot be applied right now. Please check back later.
  
    
  
    
Uh oh!
There was an error while loading. Please reload this page.