Cypress Automation Code Quality With Cypress Eslint & Prettier Plugins

Mega Mind
3 min readJun 11, 2023

--

Writing a scalable and durable code has become basic necessity for all the software engineers. Java Script has ESlint plugins to do the static code analysis, which helps us to improve the Code Quality.

For Cypress we have Cypress ESlint Plugin to do the basic and necessary code analysis.

Cypress has lots of plugins, we can use cypress eslint preprocessor and cypress eslint plugins to do the static code analysis.

Lets see how we can achieve it:

Working with cypress eslint

Step 1: Install the packages

We can install the below added packages using npm install.

  1. npm install — save-dev @cypress/browserify-preprocessor
  2. npm install eslint-plugin-cypress
  3. npm install -g eslint

Create .eslintrc.json or js file in your repo.

Read more about Cypress EsLint plugin here.

We can set the rules in .eslint.json file, as per your project needs you can configure warn/errors.

Ways To use:

  1. Using preprocessor APIs

Cypress Supports Preprocessor APIs, this preprocessor API watches the source file changes, processes them again whenever there is a change.

Whenever there is a change in the *.js file, this preprocessor will kickin and analyses the code. We can see the linting’s happening when we type our code.

All you need is add the cypressEsLint to file:preprocessor

In VSCode/IDE we can see the warnings and errors while writing the code.

2. Running from command line

In this way we can write a precommit script and add it in package.json. This gets auto triggered when we commit the code.

If you have installed eslint globally, use eslint ./ or you can use ./node_modules/.bin/eslint ./

Working with prettier

Now that I have improved the code quality in my repo, what else I can do now? Let me have a look at the code I have written, Oh! What have I written? I have improved the code but no one can understand it, because the code does not look pretty. What should I do now??

The prettier plugin will take care of this, all you need is that you need to install in to your package and see the miracles happen.

The prettier helps you in organising the code, makes the code more readable and understandable.

Similar to the Cypress EsLint plugin we can configure prettier using preprocessor API and we can write a precommit statement.

Install the below packages:

Configure prettier in the preprocessor apis.

Once prettier is configured, we can easily use them in vscode.

Happy coding :)

--

--