We recommended to use quality tools for monitoring extension code quality.
How to enable ESLint:
- Add helper script to
package.json
filepackage.json
{ "scripts": { "lint": "eslint frontend" } }
- Add
.eslintrc
file.eslintrc
{ "extends": "@plesk/eslint-config", "settings": { "react": { "pragma": "createElement" } } }
Now you can check your code with the following command:
yarn lint
Alternatively, you can run yarn lint --fix
command to automatically fix all errors that ESLint can fix.
You can also enable ESLint in your PhpStorm: Settings > Languages & Frameworks > JavaScript > Code Quality Tools > ESLint > Enable
This section is under construction.
All your code should be covered by unit tests. We recommend using Jest and Enzyme for testing your JavaScript code.
yarn add enzyme enzyme-adapter-react-16 --dev
package.json
{
"scripts": {
"pretest": "yarn lint",
"test": "plesk-ext-sdk test"
}
}
src/frontend/setupTests.js
import Enzyme from 'enzyme';
import Adapter from 'enzyme-adapter-react-16';
Enzyme.configure({ adapter: new Adapter() });
Enzyme.configure({ disableLifecycleMethods: true });
We recommended to place your tests next to the tested code in the __tests__
directory.
ext-example/
└ frontend/
├ __tests__
│ └ Overview.test.js
└ Overview.js
frontend/_tests_/Overview.test.js
import { createElement } from '@plesk/plesk-ext-sdk';
import { shallow } from 'enzyme';
import Overview from '../Overview';
describe('Overview', () => {
it('renders correctly', () => {
const wrapper = shallow(
<Overview baseUrl="/" />
);
expect(wrapper).toMatchSnapshot();
});
});
Add to .eslintrc
section env
:
{
"env": {
"jest": true
}
}
Now you can check your code via yarn test
command.
yarn test
Do not forget to add the directory frontend/__tests__/__snapshots__
to Git. It's recommended to read more about snapshot testing here.
Code coverage by tests can be collected via yarn test --coverage
command.
Code coverage is generated to coverage
folder. Do not forget to add this folder as ignored in the .gitignore
file.
.gitignore
node_modules
src/htdocs/index.php
src/htdocs/dist
coverage