We recommended to use quality tools for monitoring extension code quality.
How to enable ESLint:
-
Add helper script to
package.jsonfilepackage.json{ "scripts": { "lint": "eslint src/rontend" } } -
Add
eslint.config.mjsfile
const pleskConfig = require('@plesk/eslint-config');
module.exports = [
pleskConfig,
];- Due to dependency bug in enzyme, add version override in your
package.json
"resolutions": {
"cheerio": "1.0.0-rc.12"
}
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
To know more about ESLint and it's configuration please check ESLint documentation
All your code should be covered by unit tests. We recommend using Jest and Enzyme for testing your JavaScript code.
yarn add @cfaester/enzyme-adapter-react-18 --devpackage.json
{
"scripts": {
"pretest": "yarn lint",
"test": "plesk-ext-sdk test"
}
}src/frontend/setupTests.js
import Enzyme from 'enzyme';
import Adapter from '@cfaester/enzyme-adapter-react-18';
import { TextEncoder } from 'util';
global.TextEncoder = TextEncoder;
Enzyme.configure({ adapter: new Adapter() });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();
});
});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