This package, `babel-plugin-jsx-remove-data-test-id`, provides a Babel plugin designed to strip `data-test-id` (and `data-testid`) attributes from JSX elements during the build process, typically for production environments. It helps decouple testing concerns from production DOM, preventing test-specific attributes from bloating production bundles or being accessible in the client. The current stable version is v3.0.0, released recently with a focus on deeper object attribute removal. While there isn't a strict release cadence, the project shows active maintenance with regular updates for bug fixes, dependency security, and new feature enhancements like custom attribute stripping. Its primary differentiator is its focused approach on test-ID removal, offering configuration flexibility for custom attribute names and clear guidance on environment-specific application, preventing accidental stripping in test builds.
npm install babel-plugin-jsx-remove-data-test-idVerified import paths — ran on the pinned version, not inferred.
This quickstart shows a `.babelrc` configuration that applies the plugin only in the `production` environment, stripping `data-test-id`, `data-testid`, and a custom attribute `my-custom-test-attr`. It also includes an example React component demonstrating how these attributes would be used in code and then removed.
Upgrade `@babel/core` to version `^7.0.0`. If using Babel 6, consider installing `@babel/core@6.0.0-bridge.1` to silence peer dependency warnings, though full compatibility may vary. The recommended path is to upgrade to Babel 7.
Review your test suites and component structures if you were using `data-test-id` attributes within deeply nested object properties. Ensure all test IDs intended for removal are now stripped, and adjust tests accordingly if they relied on their presence.
Configure your Babel setup to apply `babel-plugin-jsx-remove-data-test-id` *only* in non-test environments (e.g., `production`). Utilize Babel's `env` configuration option to conditionally load the plugin.
Provide a `attributes` option in your plugin configuration with a string or an array of strings listing all custom test attribute names you wish to strip. For example: `plugins: [['babel-plugin-jsx-remove-data-test-id', { attributes: ['data-test-id', 'selenium-id'] }]]`.Upgrade your `@babel/core` package to version 7.x (e.g., `npm install @babel/core@^7.0.0`) or install `@babel/core@6.0.0-bridge.1` if you must remain on Babel 6, though an upgrade is recommended.
Modify your Babel configuration (`.babelrc` or `babel.config.js`) to ensure `babel-plugin-jsx-remove-data-test-id` is only applied in non-test environments, such as `production`. Use the `env` block in your Babel config to achieve this.