Define test ids with zero runtime cost, and extract them to a JSON file.
Install the package using:
yarn install babel-plugin-test-ids
You'll need babel >7.0.0
installed.
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
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
- String, optional, default
magicObject
- String, optional, default
$TestId
- Name of the "magic object" to look for (see below)
- String, optional, default
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"
]
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>;
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"
}
}