diff --git a/docs/rules/consistent-data-testid.md b/docs/rules/consistent-data-testid.md
index 50a4beed..f74ce83b 100644
--- a/docs/rules/consistent-data-testid.md
+++ b/docs/rules/consistent-data-testid.md
@@ -28,10 +28,10 @@ const baz = (props) =>
...
;
## 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 (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"]` |
+| 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` 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"]` |
## Example
@@ -56,3 +56,7 @@ const baz = (props) => ...
;
]
}
```
+
+## 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.
diff --git a/lib/rules/consistent-data-testid.ts b/lib/rules/consistent-data-testid.ts
index 54d7d23b..e71bcb5e 100644
--- a/lib/rules/consistent-data-testid.ts
+++ b/lib/rules/consistent-data-testid.ts
@@ -74,6 +74,12 @@ export default createTestingLibraryRule({
function getFileNameData() {
const splitPath = getFilename().split('/');
const fileNameWithExtension = splitPath.pop() ?? '';
+ if (
+ fileNameWithExtension.includes('[') ||
+ fileNameWithExtension.includes(']')
+ ) {
+ return { fileName: undefined };
+ }
const parent = splitPath.pop();
const fileName = fileNameWithExtension.split('.').shift();
diff --git a/tests/lib/rules/consistent-data-testid.test.ts b/tests/lib/rules/consistent-data-testid.test.ts
index 50c1614e..cb7487c1 100644
--- a/tests/lib/rules/consistent-data-testid.test.ts
+++ b/tests/lib/rules/consistent-data-testid.test.ts
@@ -200,6 +200,67 @@ const validTestCases: ValidTestCase[] = [
`,
options: [{ testIdPattern: 'somethingElse' }],
},
+ // To fix issue 509, https://github.com/testing-library/eslint-plugin-testing-library/issues/509
+ // Gatsby.js ja Next.js use square brackets in filenames to create dynamic routes
+ {
+ code: `
+ import React from 'react';
+
+ const TestComponent = props => {
+ return (
+
+ Hello
+
+ )
+ };
+ `,
+ options: [
+ {
+ testIdPattern: '^{fileName}(__([A-Z]+[a-z]*?)+)*$',
+ },
+ ],
+ filename: '/my/cool/file/path/[client-only].js',
+ },
+ {
+ code: `
+ import React from 'react';
+
+ const TestComponent = props => {
+ return (
+
+ Hello
+
+ )
+ };
+ `,
+ options: [
+ {
+ // should work if given the {fileName} placeholder
+ testIdPattern: '^{fileName}(__([A-Z]+[a-z]*?)+)*$',
+ },
+ ],
+ filename: '/my/cool/file/path/[...wildcard].js',
+ },
+ {
+ code: `
+ import React from 'react';
+
+ const TestComponent = props => {
+ return (
+
+ Hello
+
+ )
+ };
+ `,
+ options: [
+ {
+ // should work also if not given the {fileName} placeholder
+ testIdPattern: '^(__([A-Z]+[a-z]*?)+)*$',
+ },
+ ],
+ filename: '/my/cool/file/path/[...wildcard].js',
+ },
];
const invalidTestCases: InvalidTestCase[] = [
{