-
Notifications
You must be signed in to change notification settings - Fork 131
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(rule): add data-testid rule (#56)
* feat(rule): add data-testid rule * chore(consistent-data-testid): address pr feedback * feat(testid): support configurable attribute * chore(consistent-data-testid): update docs * chore(consistent-data-testid): fix coverage
- Loading branch information
Showing
5 changed files
with
400 additions
and
0 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,43 @@ | ||
# Enforces consistent naming for the data-testid attribute (consistent-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 | Required | Default | Details | Example | | ||
| ----------------- | -------- | ------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | -------------------------------------- | | ||
| `testIdPattern` | Yes | None | A regex used to validate the format of the `data-testid` value. `{fileName}` 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 file name is `index.js` | `^{fileName}(\_\_([A-Z]+[a-z]_?)+)_\$` | | ||
| `testIdAttribute` | No | `data-testid` | A string used to specify the attribute used for querying by ID. This is only required if data-testid has been explicitly overridden in the [RTL configuration](https://testing-library.com/docs/dom-testing-library/api-queries#overriding-data-testid) | `data-my-test-attribute` | | ||
|
||
## Example | ||
|
||
```json | ||
{ | ||
"testing-library/data-testid": [ | ||
2, | ||
{ | ||
"testIdPattern": "^TestId(__[A-Z]*)?$" | ||
} | ||
] | ||
} | ||
``` |
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,75 @@ | ||
'use strict'; | ||
|
||
const FILENAME_PLACEHOLDER = '{fileName}'; | ||
|
||
module.exports = { | ||
meta: { | ||
docs: { | ||
description: 'Ensures consistent usage of `data-testid`', | ||
category: 'Best Practices', | ||
recommended: false, | ||
}, | ||
messages: { | ||
invalidTestId: '`{{attr}}` "{{value}}" should match `{{regex}}`', | ||
}, | ||
fixable: null, | ||
schema: [ | ||
{ | ||
type: 'object', | ||
default: {}, | ||
additionalProperties: false, | ||
required: ['testIdPattern'], | ||
properties: { | ||
testIdPattern: { | ||
type: 'string', | ||
}, | ||
testIdAttribute: { | ||
type: 'string', | ||
default: 'data-testid', | ||
}, | ||
}, | ||
}, | ||
], | ||
}, | ||
|
||
create: function(context) { | ||
const { options, getFilename } = context; | ||
const { testIdPattern, testIdAttribute: attr } = options[0]; | ||
|
||
function getFileNameData() { | ||
const splitPath = getFilename().split('/'); | ||
const fileNameWithExtension = splitPath.pop(); | ||
const parent = splitPath.pop(); | ||
const fileName = fileNameWithExtension.split('.').shift(); | ||
|
||
return { | ||
fileName: fileName === 'index' ? parent : fileName, | ||
}; | ||
} | ||
|
||
function getTestIdValidator({ fileName }) { | ||
return new RegExp(testIdPattern.replace(FILENAME_PLACEHOLDER, fileName)); | ||
} | ||
|
||
return { | ||
[`JSXIdentifier[name=${attr}]`]: node => { | ||
const value = | ||
node && node.parent && node.parent.value && node.parent.value.value; | ||
const { fileName } = getFileNameData(); | ||
const regex = getTestIdValidator({ fileName }); | ||
|
||
if (value && !regex.test(value)) { | ||
context.report({ | ||
node, | ||
messageId: 'invalidTestId', | ||
data: { | ||
attr, | ||
value, | ||
regex, | ||
}, | ||
}); | ||
} | ||
}, | ||
}; | ||
}, | ||
}; |
Oops, something went wrong.