Babel plugin to provide extractable test ids with zero runtime cost.
MIT License
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:
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
undefined
magicObject
$TestId
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 filepackage.json
{
"scripts": {
"glob": "glob-test-ids -i ./test-ids/src/**/*.json -o ./test-ids/master.json"
}
}