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

testidplugin

v1.0.5

Published

A Babel plugin to automatically inject `data-testid` attributes into JSX elements for testing purposes. This plugin works with React and TypeScript projects, ensuring that all relevant elements have unique and consistent `data-testid` attributes, making i

Downloads

60

Readme

testIdPlugin

A Babel plugin to automatically inject data-testid attributes into JSX elements for testing purposes. This plugin works with React and TypeScript projects, ensuring that all relevant elements have unique and consistent data-testid attributes, making it easier to write tests with libraries like Jest, Testing Library, or Cypress.

Features

  • Automatically adds data-testid attributes to JSX elements.
  • Supports prefix to customize the generated test IDs.
  • Propagates parent data-testid values for nested elements.
  • Skips elements that already have a data-testid attribute.
  • Handles Material-UI component names, converting them to HTML elements when necessary.
  • Supports JSX and TSX files.

Installation

To install this plugin, use npm or yarn:

npm install testIdPlugin --save-dev

or

yarn add testIdPlugin --dev

Configuration

You can configure the plugin by adding it to your Babel configuration (babel.config.js or .babelrc).

Example Configuration

// babel.config.js
module.exports = {
  presets: [
    '@babel/preset-react',
    '@babel/preset-typescript',
  ],
  plugins: [
    [
      'testIdPlugin',
      {
        prefix: 'my-prefix-',    // Optional: Set a custom prefix for test IDs
        include: ['src/**/*'],    // Optional: Glob pattern to include files for transformation
        exclude: ['node_modules/**'], // Optional: Glob pattern to exclude files from transformation
      },
    ],
  ],
};

Plugin Options

  • prefix (optional): A string to prefix all data-testid values. Default is an empty string.
  • include (optional): An array of file paths or glob patterns to specify which files to include for transformation. Default is all files.
  • exclude (optional): An array of file paths or glob patterns to specify which files to exclude from transformation. Default is no exclusion.

How It Works

  • The plugin works by traversing the JSX elements in your code and checking if they already have a data-testid attribute.
  • If not, it generates a unique data-testid for each element by combining its parent element's data-testid, its own tag name, and a sibling index to ensure uniqueness.
  • The plugin also handles Material-UI components, such as Box, Typography, and others, mapping them to their corresponding native HTML elements (e.g., div, span).

Example:

Given the following JSX:

function MyComponent() {
  return (
    <div>
      <Typography>
        <FormControlLabel />
      </Typography>
    </div>
  );
}

The plugin would transform it to:

function MyComponent() {
  return (
    <div data-testid="div">
      <span data-testid="div-typography">
        <label data-testid="div-typography-label" />
      </span>
    </div>
  );
}

Customizing the Generated Test IDs

  • The generated test ID combines the data-testid of the parent element, the element's tag name, and a sibling index to ensure uniqueness.
  • Example format: "parentTestId-elementName-siblingIndex".

If a parent data-testid is present, the plugin will propagate it to child elements.

Example Project Setup

  1. Create a babel.config.js file in your project root.
touch babel.config.js
  1. Add the following configuration to the babel.config.js file:
module.exports = {
  presets: [
    '@babel/preset-react',
    '@babel/preset-typescript',
  ],
  plugins: [
    [
      'testIdPlugin',
      {
        prefix: 'test-',    // Optional: Custom prefix for test IDs
      },
    ],
  ],
};
  1. Run Babel to transform your JSX/TSX files:
npx babel src --out-dir dist
  1. Your JSX files will now include data-testid attributes.

Development

To contribute to this plugin, follow these steps:

  1. Clone the repository:
git clone https://github.com/your-username/testIdPlugin.git
cd testIdPlugin
  1. Install dependencies:
npm install
  1. Make changes and test the plugin.

  2. To publish a new version, run:

npm version patch # or minor/major
npm publish

License

This plugin is open-source software, licensed under the MIT License.