babel-plugin-react-data-testid

babel plugin for react data-testid attributes

Usage no npm install needed!

<script type="module">
  import babelPluginReactDataTestid from 'https://cdn.skypack.dev/babel-plugin-react-data-testid';
</script>

README

babel-plugin-react-data-testid

Build Status tested with jest styled with prettier All Contributors

babel plugin for react data-testid attributes

Install

$ npm install --save-dev babel-plugin-react-data-testid
$ yarn add --dev babel-plugin-react-data-testid

Usage

.babelrc.json

{
  "plugins": ["react-data-testid"]
}

Before:

function Div() {
  return <div />
}

const Hello = () => <div>hello</div>

After:

function Div() {
  return <div data-testid="Div" />
}

const Hello = () => <div data-testid="Hello">hello</div>

Note: Does not support class components.

Options

attributes

By default attributes with name data-testid will be added. You can also define custom attribute names via plugin options in your babel config.

.babelrc.json

{
  "plugins": [["react-data-testid", { "attributes": ["data-cy"] }]]
}
function Div() {
  return <div />
}

const Hello = () => <div>hello</div>

After:

function Div() {
  return <div data-cy="Div" />
}

const Hello = () => <div data-cy="Hello">hello</div>

If you need to add multiple attributes, you can define an attributes array as follows:

{
  "plugins": [
    ["react-data-testid", { "attributes": ["data-testid", "data-cy"] }]
  ]
}

Contributors ✨

Thanks goes to these wonderful people (emoji key):


akameco

💻 📖 🚇 ⚠️

This project follows the all-contributors specification. Contributions of any kind welcome!

License

MIT © akameco