Detalhes do pacote

strip-test-selectors

mainmatter87.7kMIT0.1.0

Enabling better Test selectors in Ember.js applications.

readme (leia-me)

strip-test-selectors

Enabling better element selectors in Ember.js tests

[!NOTE] ember-test-selectors monorepo was written and is maintained by Mainmatter and contributors. We offer consulting, training, and team augmentation for Ember.js – check out our website to learn more!

Features

strip-test-selectors provides Babel plugins to strip data-test-* selectors from your templates and objects:

  • Removes attributes starting with data-test- from HTML tags and component/helper invocations in your templates.

  • Removes properties starting with data-test- from your JS objects like component classes.

More information on why that is useful are available on our blog!

Compatibility

In an Embroider+Vite app:

  • Ember 3.28 or above
  • Ember CLI 4.12 or above
  • Node.js 18 or above

If your Ember app is a classic app, you can use ember-test-selectors classic addon, which supports Ember 3.8 to Ember 4.12.

Installation

pnpm add -D strip-test-selectors

Usage

In your templates, you can use data-test-* attributes and get them removed from production builds by configuring Babel (see next section):

<article>
  <h1 data-test-post-title data-test-resource-id={{post.id}}>{{post.title}}</h1>
  <p>{{post.body}}</p>
  <button data-test-like-button>Like</button>
</article>

Once you've done that you can use attribute selectors to look up and interact with those elements:

assert.dom('[data-test-post-title]').hasText('Ember is great!');

await click('[data-test-like-button]');

Usage with Components

You can use the same syntax also for component invocations:

<Spinner @color="blue" data-test-spinner>

Inside the Spinner component template the data-test-spinner attribute will be applied to the element that has ...attributes on it, or on the component wrapper div element if you don't use tagName = ''.

Usage in Ember addons

If you want to use ember-test-selectors in an addon make sure that it appears in the dependencies section of the package.json file, not in the devDependencies. This ensures that the selectors are also stripped correctly even if the app that uses the addon does not use ember-test-selectors itself.

Configuration

In an Embroider+Vite app, you are responsible for the Babel configuration of your app. Therefore, it's up to you to configure the conditions for strip-test-selectors to run. Here is a minimalist example to strip data-test-* for production build with an environment variable (which reproduces the behavior of the classic addon):

package.json:

"scripts": {
  "build": "STRIP_TEST_SELECTORS=true vite build",
  "test": "vite build --mode test && ember test --path dist"
}

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: [...],
        transforms: [
          ...templateCompatSupport(),
          // AST Transform for templates:
          // The default export of strip-test-selectors is the plugin that strips
          // data-test-* from ember templates.
          ...(process.env.STRIP_TEST_SELECTORS ? ['strip-test-selectors'] : []),
        ],
      },
    ],
    ...babelCompatSupport(),
    // JS plugin:
    // This is the additional plugin exported by strip-test-selectors that strips
    // data-test-* from JS component properties. You don't have to import it and 
    // configure it if you don't need this behavior.
    ...(process.env.STRIP_TEST_SELECTORS ? [stripPropertiesPlugin()] : []),
  ],
};

If you prefer not to customize the commands in package.json with specific environment variables, there are other ways to achieve the same thing using what already exists in process.env. For instance, you can read Vite "mode" and "NODE_ENV". Here is an example that uses minimist to access the mode argument:

// babel.config.cjs

const { mode } = require('minimist')(process.argv.slice(2));
const STRIP = process.env.NODE_ENV === 'production' && mode !== 'test';

module.exports = {
  plugins: [
    [
      'babel-plugin-ember-template-compilation',
      {
        compilerPath: 'ember-source/dist/ember-template-compiler.js',
        enableLegacyModules: [...],
        transforms: [
          ...templateCompatSupport(),
          ...(STRIP ? ['strip-test-selectors'] : []),
        ],
      },
    ],
    ...babelCompatSupport(),
    ...(STRIP ? [stripPropertiesPlugin()] : []),
  ],
};

(Note the behavior of the classic addon ember-test-selectors relied on the "Ember environment" that is not accessible at the Babel config level. Ember environment is "development" when you visit localhost:4200, "test" when you visit localhost:4200/tests, "production" when you do a Vite build, "test" when you do a Vite build and specify --mode test.)

License

ember-test-selectors is developed by and © Mainmatter GmbH and contributors. It is released under the MIT License.

ember-test-selectors is not an official part of Ember.js and is not maintained by the Ember.js Core Team.

changelog (log de mudanças)

Changelog

Release (2025-04-09)

  • ember-test-selectors 7.1.0 (minor)
  • strip-test-selectors 0.1.0 (minor)

:rocket: Enhancement

:house: Internal

Committers: 1

v7.0.0 (2024-07-25)

:boom: Breaking Change

:rocket: Enhancement

:bug: Bug Fix

:house: Internal

Committers: 6

v6.0.0 (2021-07-05)

:boom: Breaking Change

  • #764 Remove deprecated automatic attribute binding (@Turbo87)
  • #756 Drop support for Node.js 10 (@Turbo87)
  • #757 Remove support for ember-cli-babel v5 (@Turbo87)
  • #759 Stop testing on non-LTS Ember releases before v3.24 (@Turbo87)
  • #758 Remove deprecated TransformTestSelectorParamsToHashPairs transform (@Turbo87)

:house: Internal

Committers: 1

v5.5.0 (2021-07-02)

:rocket: Enhancement

  • #751 Deprecate TransformTestSelectorParamsToHashPairs transform (@Turbo87)
  • #752 Deprecate ember-cli-babel v5.x support (@Turbo87)

:memo: Documentation

:house: Internal

Committers: 1

v5.4.0 (2021-06-24)

:rocket: Enhancement

  • #742 Fix caching warnings when running under Embroider (@rwjblue)

Committers: 1

v5.3.0 (2021-05-25)

:rocket: Enhancement

  • #720 Resolve class based transform deprecation (@chancancode)
  • #721 Add patchClassicComponent option to avoid deprecation warnings (@mydea)

Committers: 2

v5.2.0 (2021-05-14)

:rocket: Enhancement

Committers: 1

v5.1.0 (2021-05-07)

:rocket: Enhancement

  • #700 Convert class based template compilation plugin to functional style (@Turbo87)

:house: Internal

Committers: 1

v5.0.0 (2020-09-02)

This release drops support for Ember.js versions below v3.8 due to an update of ember-cli-htmlbars which did the same. Other than that, no further breaking changes are contained in the release.

:boom: Breaking Change

  • #527 Bump ember-cli-htmlbars from 4.3.1 to 5.1.2 (@marcoow)

:rocket: Enhancement

  • #587 bind-data-test-attributes: Avoid deprecated getWithDefault() call (@Turbo87)

:memo: Documentation

  • #588 Update Node.js and Ember.js support documentation (@Turbo87)

Committers: 2

v4.1.0 (2020-05-07)

:rocket: Enhancement

  • #521 Ensure test selector stripping works for inline template compilation and co-located components (@rwjblue)

Committers: 1

v4.0.0 (2020-02-17)

:boom: Breaking Change

:house: Internal

Committers: 1

v3.0.0 (2019-11-22)

:boom: Breaking Change

The main breaking changes in this major release are dropping support for older Ember.js, Ember CLI and Node.js versions. The v3.x series will have the following minimum requirements:

  • Ember.js 2.16 or above
  • Ember CLI 2.14 or above
  • Node.js 8 or above

  • #341 Adjust Ember.js support range to 2.16+ (@Turbo87)

  • #338 Use setupPreprocessorRegistry() hook to register HTMLBars AST plugin (@Turbo87)
  • #337 Drop support for Node.js 4 and 6 (@Turbo87)

:rocket: Enhancement

:bug: Bug Fix

  • #359 Revert "Use setupPreprocessorRegistry() hook to register HTMLBars AST plugin" (@Turbo87)

:memo: Documentation

:house: Internal

Committers: 3

v2.1.0 (2019-03-15)

:rocket: Enhancement

:bug: Bug Fix

:house: Internal

  • #315 tests: Check that link-to bindings work properly (@Turbo87)
  • #285 TravisCI: Remove deprecated sudo: false option (@Turbo87)

Committers: 3

v2.0.0 (2018-10-23)

:boom: Breaking Change

:rocket: Enhancement

:house: Internal

Committers: 2

v1.0.0 (2018-04-26)

:boom: Breaking Change

:house: Internal

Committers: 2

v0.3.9 (2018-04-03)

:rocket: Enhancement

:house: Internal

Committers: 2

v0.3.8 (2017-11-15)

:bug: Bug Fix

  • #155 Fix HBS transforms for Ember 1.11/1.12. (@Turbo87)
  • #150 bugfix(transform): Disable handlebars transform pre-Ember-1.13. (@pzuraq)

:memo: Documentation

  • #156 Generate CHANGELOG using lerna-changelog. (@Turbo87)
  • #141 make note about positional params. (@kellyselden)
  • #140 Added a link to the deprecations warning to point to a codemod to help address it. (@lorcan)

:house: Internal

  • #154 Skip positional params stripping tests on Ember versions without reliable support. (@Turbo87)
  • #152 add acceptance test for params-to-hash-pairs transform. (@marcoow)
  • #147 Update "yarn.lock" file and "ember-cli-babel". (@Turbo87)

Committers: 5

v0.3.7 (2017-08-04)

:rocket: Enhancement

:bug: Bug Fix

:house: Internal

Committers: 3

v0.3.6 (2017-07-11)

:rocket: Enhancement

:bug: Bug Fix

Committers: 1

v0.3.5 (2017-07-06)

:bug: Bug Fix

  • #128 Add cacheKey() methods to template transforms. (@Turbo87)

Committers: 1

v0.3.4 (2017-05-30)

:bug: Bug Fix

:house: Internal

  • #114 chore(package): update ember-cli to version 2.13.2. (@Turbo87)
  • #113 chore(package): update ember-cli-htmlbars to version 2.0.1. (@Turbo87)
  • #108 Update "ember-cli" to v2.13.0. (@Turbo87)

Committers: 1

v0.3.3 (2017-04-24)

:rocket: Enhancement

:bug: Bug Fix

:memo: Documentation

Committers: 3

v0.3.2 (2017-04-07)

:bug: Bug Fix

  • #92 Revert "Switch to treeForAddonTestSupport". (@Turbo87)

Committers: 1

v0.3.1 (2017-04-07)

:rocket: Enhancement

  • #89 Switch to treeForAddonTestSupport. (@raido)

:bug: Bug Fix

  • #88 fix: strip data-test-* attributes without explicit value from production build. (@raido)

:memo: Documentation

Committers: 2

v0.3.0 (2017-03-27)

:rocket: Enhancement

:bug: Bug Fix

:house: Internal

  • #84 update ember-cli and other libraries. (@marcoow)
  • #64 Import ESLint config from eslint-config-simplabs. (@Turbo87)

Committers: 3

v0.2.1 (2017-01-25)

:bug: Bug Fix

  • #59 attributeBindings is now frozen in debug builds on v2.11, slice it before pushing. (@bgentry)

:house: Internal

  • #63 dummy/components: Explicitly set attributeBindings to [] to test freezing. (@Turbo87)
  • #61 Upgrade to ember-cli v2.11.0. (@Turbo87)
  • #60 ember-try: Remove scenarios and rely entirely on "versionCompatibility". (@Turbo87)

Committers: 2

v0.2.0 (2017-01-23)

:rocket: Enhancement

  • #57 Add support for component data-test-* attributes without values. (@Turbo87)

:bug: Bug Fix

:house: Internal

  • #58 GreenKeeper: Ignore "babel-core" updates. (@Turbo87)

Committers: 1

v0.1.1 (2017-01-17)

:rocket: Enhancement

  • #53 Add warning when tagName is empty and data-test-* attributes are used. (@Turbo87)
  • #52 Replace initializer with IIFE in the vendor tree. (@Turbo87)

:bug: Bug Fix

  • #50 add blueprint + test helper for integration tests. (@bgentry)

Committers: 2

v0.1.0 (2017-01-12)

:rocket: Enhancement

  • #48 Updated and simplified README file. (@Turbo87)
  • #45 Add Babel 5 plugin stripping "data-test-*" properties. (@Turbo87)
  • #43 Simplify testSelector() import. (@Turbo87)
  • #27 Auto bind component data-test-* attributes. (@marcoow)
  • #42 Introduce "strip" and deprecate "environments" option. (@Turbo87)
  • #40 Strip "data-test-" attributes from component and helper invocations. (@Turbo87)
  • #37 Simplify AST walker and remove "lodash" dependency. (@Turbo87)
  • #36 Run tests with and without stripping test selectors. (@Turbo87)
  • #25 Simplify testSelector() function. (@Turbo87)

:bug: Bug Fix

:memo: Documentation

:house: Internal

Committers: 4

0.0.5 (2016-11-23)

:rocket: Enhancement

:bug: Bug Fix

Committers: 2

0.0.4 (2016-09-06)

:bug: Bug Fix

:memo: Documentation

Committers: 2

0.0.3 (2016-04-08)

:rocket: Enhancement

  • #14 Add ability to specify envs to strip selectors.. (@blimmer)

:memo: Documentation

:house: Internal

  • #16 Remove duplicate repository field in package.json. (@marcoow)

Committers: 3

0.0.2 (2016-03-21)

:rocket: Enhancement

Committers: 2

0.0.1 (2016-03-04)

:rocket: Enhancement

  • #7 Add the plugin to the registry when in production environment. (@pangratz)

:bug: Bug Fix

:memo: Documentation

:house: Internal

Committers: 2