Skip to content

consistent-data-testid: Wrong {fileName} value in testIdPattern on Windows #782

Description

@pierrecholhot

Have you read the Troubleshooting section?

Yes

Plugin version

v5.11.0

ESLint version

v8.35.0

Node.js version

v16.20.1

package manager and version

v8.19.4

Operating system

Windows 11

Bug description

Context

In the plugin consistent-data-testid we are able to use the following placeholder {fileName} inside the testIdPattern so that the test ids include the file name.

Issue

On Windows 11, the value of the fileName is wrong as it contains the full path to the file.

Example value

Example of the wrong file name:

C:\workspace\projectX\src\components\Link\Link

Expected value

Expected file name:

Link

Possible cause

This might be due to the way the fileName is parsed by the plugin.

const splitPath = getFilename().split('/');

When I test with the following on Windows, it works:

const splitPath = getFilename().split('\\');

Steps to reproduce

See bug description

Error output/screenshots

No response

ESLint configuration

{
  "root": true,
  "env": {
    "browser": true,
    "node": true
  },
  "parserOptions": {
    "ecmaVersion": "latest",
    "sourceType": "module"
  },
  "parser": "@typescript-eslint/parser",
  "plugins": ["prettier", "@typescript-eslint", "react"],
  "extends": [
    "eslint:recommended",
    "plugin:@typescript-eslint/eslint-recommended",
    "plugin:@typescript-eslint/recommended",
    "plugin:react/recommended",
    "plugin:react-hooks/recommended",
    "plugin:prettier/recommended",
    "plugin:testing-library/react",
    "plugin:jest-dom/recommended"
  ],
  "settings": {
    "react": { "version": "detect" }
  },
  "rules": {
    "testing-library/consistent-data-testid": [
      "error",
      {
        "testIdAttribute": "data-testid",
        "testIdPattern": "^{fileName}__([A-Z]+[a-z])+$", /* <--- fileName here */
      }
    ]
  }
}

Rule(s) affected

testing-library/consistent-data-testid

Anything else?

No response

Do you want to submit a pull request to fix this bug?

No

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions