testing-library / testing-library/eslint-plugin-testing-library

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

Open
#782 0 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

bug
Dominant language
TypeScript
Stars
1.1k
Forks
168
Avg merge
15h 44m
Merged PRs (30d)
14

Description

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

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start at the implementation of the consistent-data-testid rule and reproduce the {fileName} handling on Windows using the configuration shown. Verify that the placeholder resolves to the basename, such as Link, rather than the full path, and add or update coverage for the Windows path case if the rule's tests expose it.

Written by the indexing model from the issue text.

Assessment

Tech stack
eslint, typescript
Domain
testing-qa, tooling
Issue type
Bug
Difficulty
2/5
Estimated time
1-3 hours
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.