-
Notifications
You must be signed in to change notification settings - Fork 154
feat(rule): add data-testid rule #56
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
Merged
Changes from 1 commit
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
d3d38b6
feat(rule): add data-testid rule
tknickman c7aa60f
chore(consistent-data-testid): address pr feedback
tknickman 6ebcb4a
feat(testid): support configurable attribute
tknickman 4dca4f0
chore(consistent-data-testid): update docs
tknickman e171edf
chore(consistent-data-testid): fix coverage
tknickman 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,46 @@ | ||
| # Enforces consistent naming for the data-testid attribute (data-testid) | ||
|
|
||
| Ensure `data-testid` values match a provided regex. This rule is un-opinionated, and requires configuration. | ||
|
|
||
| ## Rule Details | ||
|
|
||
| > Assuming the rule has been configured with the following regex: `^TestId(\_\_[A-Z]*)?$` | ||
| Examples of **incorrect** code for this rule: | ||
|
|
||
| ```js | ||
| const foo = props => <div data-testid="my-test-id">...</div>; | ||
| const foo = props => <div data-testid="myTestId">...</div>; | ||
| const foo = props => <div data-testid="TestIdEXAMPLE">...</div>; | ||
| ``` | ||
|
|
||
| Examples of **correct** code for this rule: | ||
|
|
||
| ```js | ||
| const foo = props => <div data-testid="TestId__EXAMPLE">...</div>; | ||
|
|
||
| const bar = props => <div data-testid="TestId">...</div>; | ||
|
|
||
| const baz = props => <div>...</div>; | ||
| ``` | ||
|
|
||
| ## Options | ||
|
|
||
| | Option | Details | Example | | ||
| | ------------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | --------------------------------------------- | | ||
| | testIdPattern | A regex used to validate the format of the `data-testid` value. `{componentName}` can optionally be used as a placeholder and will be substituted with the name of the file OR the name of the files parent directory in the case when the fileName is `index.js` | `'^{componentName}(\_\_([A-Z]+[a-z]_?)+)_\$'` | | ||
tknickman marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| | excludePaths | An array of path strings to exclude from the check | `["__tests__"]` | | ||
tknickman marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| ## Example | ||
|
|
||
| ```json | ||
| { | ||
| "testing-library/data-testid": [ | ||
| 2, | ||
| { | ||
| "testIdPattern": "^TestId(__[A-Z]*)?$", | ||
| "excludePaths": ["__tests__"] | ||
| } | ||
| ] | ||
| } | ||
| ``` | ||
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,81 @@ | ||
| 'use strict'; | ||
|
|
||
| module.exports = { | ||
| meta: { | ||
| docs: { | ||
| description: 'Ensures consistent usage of `data-testid`', | ||
| category: 'Best Practices', | ||
| recommended: false, | ||
| }, | ||
| messages: { | ||
| invalidTestId: '`data-testid` "{{value}}" should match `{{regex}}`', | ||
| }, | ||
| fixable: null, | ||
| schema: [ | ||
| { | ||
| type: 'object', | ||
| additionalProperties: false, | ||
| properties: { | ||
| testIdPattern: { | ||
| type: 'string', | ||
| default: '', | ||
| }, | ||
| excludePaths: { | ||
| type: 'array', | ||
| items: { type: 'string' }, | ||
| default: [], | ||
| }, | ||
| }, | ||
| }, | ||
| ], | ||
| }, | ||
|
|
||
| create: function(context) { | ||
| const { options, getFilename } = context; | ||
| const defaultOptions = { testIdPattern: '', excludePaths: [] }; | ||
| const ruleOptions = options.length ? options[0] : defaultOptions; | ||
|
|
||
| function getComponentData() { | ||
| const splitPath = getFilename().split('/'); | ||
| const exclude = ruleOptions.excludePaths.some(path => | ||
| splitPath.includes(path) | ||
| ); | ||
| const fileNameWithExtension = splitPath.pop(); | ||
| const parent = splitPath.pop(); | ||
| const fileName = fileNameWithExtension.split('.').shift(); | ||
|
|
||
| return { | ||
| componentDescriptor: fileName === 'index' ? parent : fileName, | ||
| exclude, | ||
| }; | ||
| } | ||
|
|
||
| function getTestIdValidator({ componentName }) { | ||
| return new RegExp( | ||
| ruleOptions.testIdPattern.replace('{componentName}', componentName) | ||
| ); | ||
| } | ||
|
|
||
| return { | ||
| 'JSXIdentifier[name=data-testid]': node => { | ||
| const { value } = (node && node.parent && node.parent.value) || {}; | ||
| const { | ||
| componentDescriptor: componentName, | ||
| exclude, | ||
| } = getComponentData(); | ||
| const regex = getTestIdValidator({ componentName }); | ||
|
|
||
| if (!exclude && value && !regex.test(value)) { | ||
| context.report({ | ||
| node, | ||
| messageId: 'invalidTestId', | ||
| data: { | ||
| value, | ||
| regex, | ||
| }, | ||
| }); | ||
| } | ||
| }, | ||
| }; | ||
| }, | ||
| }; |
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,217 @@ | ||
| 'use strict'; | ||
|
|
||
| // ------------------------------------------------------------------------------ | ||
| // Requirements | ||
| // ------------------------------------------------------------------------------ | ||
|
|
||
| const rule = require('../../../lib/rules/data-testid'); | ||
| const RuleTester = require('eslint').RuleTester; | ||
|
|
||
| // ------------------------------------------------------------------------------ | ||
| // Tests | ||
| // ------------------------------------------------------------------------------ | ||
|
|
||
| const parserOptions = { | ||
| ecmaVersion: 2018, | ||
| sourceType: 'module', | ||
| ecmaFeatures: { | ||
| jsx: true, | ||
| }, | ||
| }; | ||
|
|
||
| const ruleTester = new RuleTester({ parserOptions }); | ||
| ruleTester.run('data-testid', rule, { | ||
| valid: [ | ||
| { | ||
| code: ` | ||
| import React from 'react'; | ||
|
|
||
| const TestComponent = props => { | ||
| return ( | ||
| <div data-testid="cool"> | ||
| Hello | ||
| </div> | ||
| ) | ||
| }; | ||
| `, | ||
| options: [], | ||
| }, | ||
| { | ||
| code: ` | ||
| import React from 'react'; | ||
|
|
||
| const TestComponent = props => { | ||
| return ( | ||
| <div data-testid="cool"> | ||
| Hello | ||
| </div> | ||
| ) | ||
| }; | ||
| `, | ||
| options: [{ testIdPattern: 'cool' }], | ||
| }, | ||
| { | ||
| code: ` | ||
| import React from 'react'; | ||
|
|
||
| const TestComponent = props => { | ||
| return ( | ||
| <div className="cool"> | ||
| Hello | ||
| </div> | ||
| ) | ||
| }; | ||
| `, | ||
| options: [{ testIdPattern: 'cool' }], | ||
| }, | ||
| { | ||
| code: ` | ||
| import React from 'react'; | ||
|
|
||
| const TestComponent = props => { | ||
| return ( | ||
| <div data-testid="Awesome__CoolStuff"> | ||
| Hello | ||
| </div> | ||
| ) | ||
| }; | ||
| `, | ||
| options: [ | ||
| { | ||
| testIdPattern: '^{componentName}(__([A-Z]+[a-z]*?)+)*$', | ||
| }, | ||
| ], | ||
| filename: '/my/cool/file/path/Awesome.js', | ||
| }, | ||
| { | ||
| code: ` | ||
| import React from 'react'; | ||
|
|
||
| const TestComponent = props => { | ||
| return ( | ||
| <div data-testid="Awesome"> | ||
| Hello | ||
| </div> | ||
| ) | ||
| }; | ||
| `, | ||
| options: [ | ||
| { | ||
| testIdPattern: '^{componentName}(__([A-Z]+[a-z]*?)+)*$', | ||
| }, | ||
| ], | ||
| filename: '/my/cool/file/path/Awesome.js', | ||
| }, | ||
| { | ||
| code: ` | ||
| import React from 'react'; | ||
|
|
||
| const TestComponent = props => { | ||
| return ( | ||
| <div data-testid="Parent"> | ||
| Hello | ||
| </div> | ||
| ) | ||
| }; | ||
| `, | ||
| options: [ | ||
| { | ||
| testIdPattern: '^{componentName}(__([A-Z]+[a-z]*?)+)*$', | ||
| }, | ||
| ], | ||
| filename: '/my/cool/file/Parent/index.js', | ||
| }, | ||
| { | ||
| code: ` | ||
| import React from 'react'; | ||
|
|
||
| const TestComponent = props => { | ||
| return ( | ||
| <div data-testid="Parent"> | ||
| Hello | ||
| </div> | ||
| ) | ||
| }; | ||
| `, | ||
| options: [ | ||
| { | ||
| testIdPattern: '^{componentName}(__([A-Z]+[a-z]*?)+)*$', | ||
| excludePaths: ['__tests__'], | ||
| }, | ||
| ], | ||
| filename: '/my/cool/__tests__/Parent/index.js', | ||
| }, | ||
| ], | ||
| invalid: [ | ||
| { | ||
| code: ` | ||
| import React from 'react'; | ||
|
|
||
| const TestComponent = props => { | ||
| return ( | ||
| <div data-testid="Awesome__CoolStuff"> | ||
| Hello | ||
| </div> | ||
| ) | ||
| }; | ||
| `, | ||
| options: [{ testIdPattern: 'error' }], | ||
| errors: [ | ||
| { | ||
| message: '`data-testid` "Awesome__CoolStuff" should match `/error/`', | ||
| }, | ||
| ], | ||
| }, | ||
| { | ||
| code: ` | ||
| import React from 'react'; | ||
|
|
||
| const TestComponent = props => { | ||
| return ( | ||
| <div data-testid="Nope"> | ||
| Hello | ||
| </div> | ||
| ) | ||
| }; | ||
| `, | ||
| options: [ | ||
| { | ||
| testIdPattern: 'matchMe', | ||
| excludePaths: ['__mocks__'], | ||
| }, | ||
| ], | ||
| filename: '/my/cool/__tests__/Parent/index.js', | ||
| errors: [ | ||
| { | ||
| message: '`data-testid` "Nope" should match `/matchMe/`', | ||
| }, | ||
| ], | ||
| }, | ||
| { | ||
| code: ` | ||
| import React from 'react'; | ||
|
|
||
| const TestComponent = props => { | ||
| return ( | ||
| <div data-testid="WrongComponent__cool"> | ||
| Hello | ||
| </div> | ||
| ) | ||
| }; | ||
| `, | ||
| options: [ | ||
| { | ||
| testIdPattern: '^{componentName}(__([A-Z]+[a-z]*?)+)*$', | ||
| excludePaths: ['__mocks__'], | ||
| }, | ||
| ], | ||
| filename: '/my/cool/__tests__/Parent/index.js', | ||
| errors: [ | ||
| { | ||
| message: | ||
| '`data-testid` "WrongComponent__cool" should match `/^Parent(__([A-Z]+[a-z]*?)+)*$/`', | ||
| }, | ||
| ], | ||
| }, | ||
| ], | ||
| }); |
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.