strip-test-selectors

raw JSON →
0.1.0 verified Sat Apr 25 auth: no javascript

A Babel plugin that strips data-test-* attributes from Ember.js templates and properties from JavaScript objects in production builds. Current stable version is 0.1.0, released as part of the ember-test-selectors monorepo. It is designed for Embroider+Vite apps (Ember 3.28+, Ember CLI 4.12+, Node.js 18+). Unlike the classic ember-test-selectors addon which works with classic Ember builds, this package provides granular Babel plugins for Vite-based setups, giving developers control over when stripping occurs via environment variables.

error TypeError: Cannot destructure property 'stripPropertiesPlugin' of 'require(...)' as it is undefined.
cause Using old named import syntax for default export or incorrect package path.
fix
Use stripPropertiesPlugin as a named export: import { stripPropertiesPlugin } from 'strip-test-selectors'
error Error: Cannot find module 'babel-plugin-ember-template-compilation'
cause Missing peer dependency babel-plugin-ember-template-compilation.
fix
Install it: npm install -D babel-plugin-ember-template-compilation
error AssertionError [ERR_ASSERTION]: The plugin expected an object for 'transforms', got undefined.
cause babel-plugin-ember-template-compilation config missing transforms array.
fix
Provide transforms array in plugin options, e.g., transforms: [stripTestSelectors]
gotcha This package only works with Embroider+Vite apps, not classic Ember builds. Classic builds must use ember-test-selectors addon.
fix Use ember-test-selectors package for classic Ember apps, or migrate to Embroider+Vite.
gotcha The default export is the AST transform for templates, not a named export. Doing import { stripTestSelectors } will fail with 'undefined'.
fix Use import stripTestSelectors from 'strip-test-selectors' for the template plugin.
breaking Requires Node.js 18+ and Ember 3.28+. Older versions are not supported.
fix Update Node.js to 18+ and Ember to 3.28+.
gotcha You are responsible for configuring when stripping occurs via environment variable; there is no automatic production detection.
fix Set STRIP_TEST_SELECTORS=true in your production build script.
gotcha The plugin only strips attributes starting with 'data-test-'. Custom prefixes or suffixes are not supported.
fix Use data-test-* prefix for all test selectors.
npm install strip-test-selectors
yarn add strip-test-selectors
pnpm add strip-test-selectors

Configures Babel to strip data-test-* from templates and JS objects only when STRIP_TEST_SELECTORS environment variable is set, typically for production builds.

// babel.config.cjs
const { stripPropertiesPlugin } = require('strip-test-selectors');

module.exports = {
  plugins: [
    [
      'babel-plugin-ember-template-compilation',
      {
        compilerPath: 'ember-source/dist/ember-template-compiler.js',
        enableLegacyModules: ['@ember/template-compiler'],
        transforms: [
          ...(process.env.STRIP_TEST_SELECTORS ? [require('strip-test-selectors').default] : []),
        ],
      },
    ],
    ...(process.env.STRIP_TEST_SELECTORS ? [stripPropertiesPlugin] : []),
  ],
};