Security News
JSR Working Group Kicks Off with Ambitious Roadmap and Plans for Open Governance
At its inaugural meeting, the JSR Working Group outlined plans for an open governance model and a roadmap to enhance JavaScript package management.
babel-plugin-transform-react-qa-classes
Advanced tools
Add component's name in `data-qa` attributes to React Components Edit
This babel plugin adds the component name as a data-qa
in each React Component.
Before | After |
---|---|
class componentName extends Component { render () { return ( <div> <div>Hello world</div> </div> ) } } |
class componentName extends Component { render () { return ( <div data-qa='component-name'> <div>Hello world</div> </div> ) } } |
This plugin asumes that you are using React and Babel as a building tool to generate your bundle.
The idea is to facilitate Automate Testing on Frontend Applications. Automate Frontend highly requires to get the DOMElements and interact with them, adding data-qa
attributes automatically to all the components will make it more easy, rather than do it by code, with this way you won't have this data-qa
in production code.
On the testing site would need to get the element like that:
document.querySelectorAll('[data-qa="component"]')
That depends on the Test suit stack, for example with Ruby and PageObject
looks like that:
div(:component, data_qa: 'component')
npm install --save-dev babel-plugin-transform-react-qa-classes
# or yarn add -D
Inside .babelrc
:
{
"presets": ["es2015", "react"],
"env": {
"dev": {
"plugins": ["transform-react-qa-classes"]
}
}
}
Note: Adding this plugin only on
DEV
mode (orPREPROD
) allows not havingdata-qa
attributes on production.
You can specify the format of the name that you want and the name of the attribute, inside your babelrc
:
{
"presets": ["es2015", "react"],
"env": {
"dev": {
"plugins": ["transform-react-qa-classes", {
"attribute": "qa-property",
"format": "camel"
}]
}
}
}
Note: format can be: "camel" (camelCase), "snake" (snake_case) or "kebab" (kebab-case).
babel --plugins transform-react-qa-classes component.js
require('babel-core').transform(`code`, {
plugins: ['transform-react-qa-classes']
})
PRs for additional features are welcome!
There's still a few feature that are missing, for example each change of the state of the component is added as a data-qa-state
into the DOM. Support for more libraries.
I recommend checking this handbook about how to write babel plugins in order to learn.
git clone https://github.com/davesnx/babel-plugin-transform-react-qa-classes
git remote set-url origin https://github.com/YOUR_USERNAME/babel-plugin-transform-react-qa-classes.git
git checkout -b BRANCH_NAME
In order to do the commits I prefer to use Commitizen and there's a githook setted up when you push it runs the tests.
Is your company using it? I would love to know more! Could you answer this small Typeform :P
MIT
FAQs
Add component's name in `data-qa` attributes to React Components Edit
The npm package babel-plugin-transform-react-qa-classes receives a total of 9,638 weekly downloads. As such, babel-plugin-transform-react-qa-classes popularity was classified as popular.
We found that babel-plugin-transform-react-qa-classes demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
At its inaugural meeting, the JSR Working Group outlined plans for an open governance model and a roadmap to enhance JavaScript package management.
Security News
Research
An advanced npm supply chain attack is leveraging Ethereum smart contracts for decentralized, persistent malware control, evading traditional defenses.
Security News
Research
Attackers are impersonating Sindre Sorhus on npm with a fake 'chalk-node' package containing a malicious backdoor to compromise developers' projects.