Skip to content
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

feat(consistent-data-testid): add support for custom error message #719

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions docs/rules/consistent-data-testid.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ const baz = (props) => <div>...</div>;
| ----------------- | -------- | ------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | ----------------------------------------------------- |
| `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` OR empty string in the case of dynamically changing routes (that contain square brackets) with `Gatsby.js` or `Next.js` | `^{fileName}(\_\_([A-Z]+[a-z]_?)+)_\$` |
| `testIdAttribute` | No | `data-testid` | A string (or array of strings) 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`, `["data-testid", "testId"]` |
| `customMessage` | No | `undefined` | A string used to display a custom message whenever warnings/errors are reported. | `A custom message` |

## Example

Expand All @@ -59,6 +60,17 @@ const baz = (props) => <div>...</div>;
}
```

```json
{
"testing-library/consistent-data-testid": [
2,
{
"customMessage": "A custom message"
}
]
}
```

## Notes

- If you are using Gatsby.js's [client-only routes](https://www.gatsbyjs.com/docs/reference/routing/file-system-route-api/#syntax-client-only-routes) or Next.js's [dynamic routes](https://nextjs.org/docs/routing/dynamic-routes) and therefore have square brackets (`[]`) in the filename (e.g. `../path/to/[component].js`), the `{fileName}` placeholder will be replaced with an empty string. This is because a linter cannot know what the dynamic content will be at run time.
24 changes: 21 additions & 3 deletions lib/rules/consistent-data-testid.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,14 @@ import { createTestingLibraryRule } from '../create-testing-library-rule';
import { isJSXAttribute, isLiteral } from '../node-utils';

export const RULE_NAME = 'consistent-data-testid';
export type MessageIds = 'consistentDataTestId';
export type MessageIds =
| 'consistentDataTestId'
| 'consistentDataTestIdCustomMessage';
export type Options = [
{
testIdAttribute?: string[] | string;
testIdPattern: string;
customMessage?: string;
}
];

Expand All @@ -28,6 +31,7 @@ export default createTestingLibraryRule<Options, MessageIds>({
},
messages: {
consistentDataTestId: '`{{attr}}` "{{value}}" should match `{{regex}}`',
consistentDataTestIdCustomMessage: '`{{message}}`',
},
schema: [
{
Expand All @@ -53,6 +57,10 @@ export default createTestingLibraryRule<Options, MessageIds>({
},
],
},
customMessage: {
default: undefined,
type: 'string',
},
},
},
],
Expand All @@ -61,6 +69,7 @@ export default createTestingLibraryRule<Options, MessageIds>({
{
testIdPattern: '',
testIdAttribute: 'data-testid',
customMessage: undefined,
},
],
detectionOptions: {
Expand All @@ -69,7 +78,7 @@ export default createTestingLibraryRule<Options, MessageIds>({

create: (context, [options]) => {
const { getFilename } = context;
const { testIdPattern, testIdAttribute: attr } = options;
const { testIdPattern, testIdAttribute: attr, customMessage } = options;

function getFileNameData() {
const splitPath = getFilename().split('/');
Expand Down Expand Up @@ -100,6 +109,14 @@ export default createTestingLibraryRule<Options, MessageIds>({
}
}

function getErrorMessageId(): MessageIds {
if (customMessage === undefined) {
return 'consistentDataTestId';
}

return 'consistentDataTestIdCustomMessage';
}

return {
JSXIdentifier: (node) => {
if (
Expand All @@ -118,11 +135,12 @@ export default createTestingLibraryRule<Options, MessageIds>({
if (value && typeof value === 'string' && !regex.test(value)) {
context.report({
node,
messageId: 'consistentDataTestId',
messageId: getErrorMessageId(),
data: {
attr: node.name,
value,
regex,
message: customMessage,
},
});
}
Expand Down
36 changes: 36 additions & 0 deletions tests/lib/rules/consistent-data-testid.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -283,6 +283,7 @@ const invalidTestCases: InvalidTestCase[] = [
attr: 'data-testid',
value: 'Awesome__CoolStuff',
regex: '/error/',
message: '',
},
},
],
Expand Down Expand Up @@ -312,6 +313,7 @@ const invalidTestCases: InvalidTestCase[] = [
attr: 'data-testid',
value: 'Nope',
regex: '/matchMe/',
message: '',
},
},
],
Expand Down Expand Up @@ -342,6 +344,7 @@ const invalidTestCases: InvalidTestCase[] = [
attr: 'my-custom-attr',
value: 'WrongComponent__cool',
regex: '/^Parent(__([A-Z]+[a-z]*?)+)*$/',
message: '',
},
},
],
Expand Down Expand Up @@ -380,6 +383,7 @@ const invalidTestCases: InvalidTestCase[] = [
attr: 'another-custom-attr',
value: 'wrong',
regex: '/^right$/',
message: '',
},
},
],
Expand Down Expand Up @@ -409,6 +413,38 @@ const invalidTestCases: InvalidTestCase[] = [
attr: 'data-testid',
value: 'WrongComponent__cool',
regex: '/^Parent(__([A-Z]+[a-z]*?)+)*$/',
message: '',
},
},
],
},
{
code: ` // test for custom message
import React from 'react';

const TestComponent = props => {
return (
<div data-testid="snake_case_value">
Hello
</div>
)
};
`,
options: [
{
testIdPattern: '^([a-z][a-z0-9]*)(-[a-z0-9]+)*$', //kebab-case
customMessage: 'Please use kebab-cased data-testid values.',
},
],
filename: '/my/cool/__tests__/Parent/index.js',
errors: [
{
messageId: 'consistentDataTestIdCustomMessage',
data: {
attr: 'data-testid',
value: 'snake_case_value',
regex: '^([a-z][a-z0-9]*)(-[a-z0-9]+)*$',
message: 'Please use kebab-cased data-testid values.',
},
},
],
Expand Down