npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

component-testid-validator

v1.0.9

Published

A tool to check for testID attributes in React and React Native components.

Downloads

23

Readme

Component TestID Checker

Component TestID Checker is a command-line tool that checks for the presence of testID attributes in React, React Native, and Angular components. This tool helps ensure that all interactive elements in your project have the necessary testID or also can detect multiple attributes for testing purposes and can autofix.

Installation

You can install the package globally using npm:

npm install -g component-testid-checker

Usage

Run the tool using the command:

  check-test-ids --config path/to/your-config.json

or add it in the scripts of your package.json file

  "check-test-ids": "check-test-ids --config config.json"

and run

   npm run check-test-ids

Screenshots

Alt Text

Alt Text

Configuration

The tool requires a configuration file to specify various options. The configuration file should be a JSON file with the following structure:

React Native

 {
  "directoryToCheck": "src",
  "testIdAttributes": ["testID"],
  "extensions": "js,jsx,tsx,ts",
  "outputFormat": "text",
  "excludePattern": "",
  "autoFix":true,
  "nonInteractiveElements": ["View", "Text", "Image"],
  "internalElementPattern": "^_",
  "interactiveElements": [
    "Button",
    "TouchableOpacity",
    "TextInput",
    "ScrollView",
    "FlatList"
  ],
  "dynamicTestIdFunctions": ["getTestID"],
  "chalkConfig": {
    "componentName": "red",
    "lineNumber": "yellow",
    "filePath": "blue",
    "attributeName": "green",
    "totalMissingCount": "magenta"
  }
}

React

{
  "directoryToCheck": "src",
  "testIdAttributes": ["data-testid"],
  "extensions": "js,jsx,tsx,ts",
  "outputFormat": "text",
  "autoFix":true,
  "excludePattern": "",
  "nonInteractiveElements": ["div", "span", "img"],
  "internalElementPattern": "^_",
  "interactiveElements": [
    "button",
    "a",
    "input",
    "select",
    "textarea"
  ],
  "dynamicTestIdFunctions": ["getTestID"],
  "chalkConfig": {
    "componentName": "red",
    "lineNumber": "yellow",
    "filePath": "blue",
    "attributeName": "green",
    "totalMissingCount": "magenta"
  }
}

Configuration Options

Auto Fix

  • autoFix: If you want the package to autofix the missing attributes in the actual code you can pass this attribute to be true

You will prompted with a question to autofix the missing ones

Alt Text

yes:

Alt Text

no:

Alt Text

Other Config

  • directoryToCheck (string): The directory to search for component files. Default is 'src'.
  • testIdAttributes (array of strings): The list of testID attributes to check for. Default is ['testID'].
  • extensions (string): The file extensions to check, separated by commas. Default is 'js,jsx,tsx,ts'.
  • outputFormat (string): The output format for logs, either 'text' or 'json'. Default is 'text'.
  • excludePattern (string): A glob pattern for files to exclude from the check.
  • nonInteractiveElements (array of strings): A list of non-interactive element names to exclude from the check.
  • internalElementPattern (string): A regex pattern to identify internal element names that should be excluded from the check.
  • interactiveElements (array of strings): A list of interactive element names to include in the check.
  • dynamicTestIdFunctions (array of strings): A list of function names that dynamically set testID attributes.
  • chalkConfig (object): Configuration for chalk colors. Each key represents a log element, and the value is a string specifying the chalk styles.

Example

To run the tool with a custom configuration file, use:

check-test-ids --config ./my-custom-config.json

Example Output

If the tool finds elements missing testID attributes, it will log warnings:

Warning: <Button> in src/components/MyButton.js at line 15 does not have a testID attribute.
Warning: <TouchableOpacity> in src/screens/HomeScreen.js at line 27 does not have a testID attribute.
Total elements missing testID: 2

If all elements have testID attributes, it will display a success message:

All components have testID attributes.

Additional Information

  • Non-Interactive Elements: The tool will skip checking for testID attributes on elements that are purely decorative or non-interactive.
  • Internal Elements: Elements meant to be private or internal to a component will also be skipped.
  • Elements with Stable Semantic Selectors: If an element can be reliably selected using semantic HTML, ARIA roles, or stable class names, prefer those over adding an extra testID..

Feedback

If you have any feedback, please reach out to me at [email protected]

Contribution

Contributions are welcome! For major changes, please open an issue first to discuss what you would like to change.

By Order of the Peaky Blinders!