babel-plugin-test-ids
TypeScript icon, indicating that this package has built-in type declarations

1.0.1 • Public • Published

babel-plugin-test-ids

CircleCI Test Coverage

MIT Licensed Language

NPM Dependencies

Issues Pull Requests

Define test ids with zero runtime cost, and extract them to a JSON file.

Install

Install the package using:

yarn install babel-plugin-test-ids

You'll need babel >7.0.0 installed.

Usage

There are two parts to this package:

  • A babel plugin
    • Transforms code to replace "magic" test ids with string literals
    • Extracts found test ids to JSON files
  • CLI program
    • Combines extracted test ids into a single file

Babel plugin

Setup

The plugin should be added to the plugins array in your babel configuration.

module.exports = {
    plugins: [
        "test-ids"
    ]
};

There are two available configuration options, both optional.

  • extractTo
    • String, optional, default undefined
    • Folder location to write extracted test ids
    • Relative to current working directory
    • If not defined, no files will be written
  • magicObject
    • String, optional, default $TestId
    • Name of the "magic object" to look for (see below)

Using the magic object

The $TestId magic object can now be used in your code. It can be used in plain JavaScript files, or (subject to appropriate plugins), JSX. Property access will be transformed by the plugin into a string literal containing the name of the value identifier. The value of the injected string literal will then be extracted to a JSON file, in a tree structure matching the original file.

Input (src/test/file.js)

const foo = {
    bar: $TestId.MyTestId
}

Transformed (src/test/file.js)

const foo = {
    bar: "MyTestId"
}

Extracted (test-ids/src/test/file.json)

[
    "MyTestId"
]

TypeScript

If you're using TypeScript, you'll soon find that usage as above causes build errors, because $TestId is not defined. This can be corrected by declaring a global variable, in (for example) globals.d.ts at the project root.

declare const $TestId: Record<string, string>;

CLI

Often it is desirable to have a single "master" file of test ids. This can be generated using the provided glob-test-ids program.

There are two inputs, both required:

  • -i, --ids-location Glob-style location of previously generated extracted test ids
  • -o, --output Location to output master file

package.json

{
    "scripts": {
        "glob": "glob-test-ids -i ./test-ids/src/**/*.json -o ./test-ids/master.json"
    }
}

Readme

Keywords

none

Package Sidebar

Install

npm i babel-plugin-test-ids

Weekly Downloads

3

Version

1.0.1

License

MIT

Unpacked Size

137 kB

Total Files

51

Last publish

Collaborators

  • alsiola