|
| 1 | +# Cypress ESLint Plugin - Flat Config |
| 2 | + |
| 3 | +This document supplements the [README](README.md) document and describes how to use the Cypress ESLint Plugin (`eslint-plugin-cypress`) in an ESLint flat config environment. |
| 4 | + |
| 5 | +Usage with ESLint `8.57.0` is described. An update to this document is planned to cover ESLint `9.x` additionally. |
| 6 | + |
| 7 | +## Introduction |
| 8 | + |
| 9 | +[ESLint v9.0.0](https://eslint.org/blog/2024/04/eslint-v9.0.0-released/) was released on April 5, 2024, establishing flat config as the default for this version. |
| 10 | + |
| 11 | +Previously, ESLint had announced in their blog post [Flat config rollout plans](https://eslint.org/blog/2023/10/flat-config-rollout-plans/) in October 2023 that flat config was planned to be the default in ESLint `v9.0.0` and that the eslintrc configuration system is planned to be removed in the future ESLint `v10.0.0`. |
| 12 | + |
| 13 | +The Cypress ESLint Plugin (`eslint-plugin-cypress`) is currently based on ESLint `8.x` and has not yet been migrated to support Flat Config natively. [ESLint's new config system, Part 2: Introduction to flat config](https://eslint.org/blog/2022/08/new-config-system-part-2/) from August 2022 introduced the [Backwards compatibility utility](https://eslint.org/blog/2022/08/new-config-system-part-2/#backwards-compatibility-utility). This gives the capability to use `eslint-plugin-cypress` in an ESLint flat config environment. |
| 14 | + |
| 15 | +The following information details installation and usage examples for `eslint-plugin-cypress` together with related plugins in such an ESLint `8.57.0` flat config environment. |
| 16 | + |
| 17 | +## Installation |
| 18 | + |
| 19 | +It is recommended to use a minimum ESLint `v8.x` version [[email protected]](https://github.com/eslint/eslint/releases/tag/v8.57.0). |
| 20 | + |
| 21 | +```shell |
| 22 | +npm install eslint@^8.57.0 @eslint/eslintrc eslint-plugin-cypress --save-dev |
| 23 | +``` |
| 24 | + |
| 25 | +or |
| 26 | + |
| 27 | +```shell |
| 28 | +yarn add eslint@^8.57.0 @eslint/eslintrc eslint-plugin-cypress --dev |
| 29 | +``` |
| 30 | + |
| 31 | +## Usage examples |
| 32 | + |
| 33 | +Add a flat configuration file `eslint.config.mjs` file to the root directory of your Cypress project. In the following sections, different examples of possible configuration file contents are given, together with some brief explanations. Adapt these examples according to your needs. |
| 34 | + |
| 35 | +### Cypress |
| 36 | + |
| 37 | +All rules from `eslint-plugin-cypress` are available through the `FlatCompat` class of [@eslint/eslintrc](https://www.npmjs.com/package/@eslint/eslintrc). |
| 38 | +- [cypress/unsafe-to-chain-command](https://github.com/cypress-io/eslint-plugin-cypress/blob/master/docs/rules/unsafe-to-chain-command.md) is active and set to `error` |
| 39 | + |
| 40 | +```js |
| 41 | +import { FlatCompat } from '@eslint/eslintrc' |
| 42 | +const compat = new FlatCompat() |
| 43 | +export default [ |
| 44 | + ...compat.config( |
| 45 | + { |
| 46 | + plugins: ['cypress'], |
| 47 | + rules: { |
| 48 | + 'cypress/unsafe-to-chain-command': 'error' |
| 49 | + } |
| 50 | + }) |
| 51 | +] |
| 52 | +``` |
| 53 | + |
| 54 | +### Cypress recommended |
| 55 | + |
| 56 | +The `eslint-plugin-cypress` [recommended rules](README.md#rules) `plugin:cypress/recommended` are activated, except for |
| 57 | +- [cypress/no-unnecessary-waiting](https://github.com/cypress-io/eslint-plugin-cypress/blob/master/docs/rules/no-unnecessary-waiting.md) set to `off` |
| 58 | + |
| 59 | +```js |
| 60 | +import { FlatCompat } from '@eslint/eslintrc' |
| 61 | +const compat = new FlatCompat() |
| 62 | +export default [ |
| 63 | + ...compat.config( |
| 64 | + { |
| 65 | + extends: ['plugin:cypress/recommended'], |
| 66 | + rules: { |
| 67 | + 'cypress/no-unnecessary-waiting': 'off' |
| 68 | + } |
| 69 | + }) |
| 70 | +] |
| 71 | +``` |
| 72 | + |
| 73 | +### Cypress and Mocha recommended |
| 74 | + |
| 75 | +[eslint-plugin-mocha](https://www.npmjs.com/package/eslint-plugin-mocha) is added to the previous example. This plugin offers a flat file recommended option `configs.flat.recommended`. |
| 76 | + |
| 77 | +The settings for individual `mocha` rules from the `configs.flat.recommended` option are changed. |
| 78 | +- [mocha/no-exclusive-tests](https://github.com/lo1tuma/eslint-plugin-mocha/blob/main/docs/rules/no-exclusive-tests.md) and [mocha/no-skipped-tests](https://github.com/lo1tuma/eslint-plugin-mocha/blob/main/docs/rules/no-skipped-tests.md) are set to `error` instead of `warn` |
| 79 | +- [mocha/no-mocha-arrows](https://github.com/lo1tuma/eslint-plugin-mocha/blob/main/docs/rules/no-mocha-arrows.md) is set to `off` instead of `error` |
| 80 | + |
| 81 | +```shell |
| 82 | +npm install eslint-plugin-mocha@^10.4.3 --save-dev |
| 83 | +``` |
| 84 | + |
| 85 | +```js |
| 86 | +import { FlatCompat } from '@eslint/eslintrc' |
| 87 | +import mochaPlugin from 'eslint-plugin-mocha' |
| 88 | +const compat = new FlatCompat() |
| 89 | +export default [ |
| 90 | + mochaPlugin.configs.flat.recommended, { |
| 91 | + rules: { |
| 92 | + 'mocha/no-exclusive-tests': 'error', |
| 93 | + 'mocha/no-skipped-tests': 'error', |
| 94 | + 'mocha/no-mocha-arrows': 'off' |
| 95 | + } |
| 96 | + }, |
| 97 | + ...compat.config( |
| 98 | + { |
| 99 | + extends: ['plugin:cypress/recommended'], |
| 100 | + rules: { |
| 101 | + 'cypress/no-unnecessary-waiting': 'off' |
| 102 | + } |
| 103 | + }) |
| 104 | +] |
| 105 | +``` |
| 106 | + |
| 107 | +### Cypress and Chai recommended |
| 108 | + |
| 109 | +[eslint-plugin-chai-friendly](https://www.npmjs.com/package/eslint-plugin-chai-friendly) is combined with the Cypress plugin `eslint-plugin-cypress`. |
| 110 | + |
| 111 | +The [eslint-plugin-chai-friendly](https://www.npmjs.com/package/eslint-plugin-chai-friendly) plugin does not currently offer flat config options and so the `FlatCompat` class of [@eslint/eslintrc](https://www.npmjs.com/package/@eslint/eslintrc) enables this plugin to be used too. The recommended rules for both plugins are used: `plugin:cypress/recommended` and `plugin:chai-friendly/recommended`. |
| 112 | + |
| 113 | +```shell |
| 114 | +npm install eslint-plugin-chai-friendly --save-dev |
| 115 | +``` |
| 116 | + |
| 117 | +```js |
| 118 | +import { FlatCompat } from '@eslint/eslintrc' |
| 119 | +const compat = new FlatCompat() |
| 120 | +export default [ |
| 121 | + ...compat.config( |
| 122 | + { |
| 123 | + extends: [ |
| 124 | + 'plugin:cypress/recommended', |
| 125 | + 'plugin:chai-friendly/recommended' |
| 126 | + ], |
| 127 | + rules: { |
| 128 | + 'cypress/no-unnecessary-waiting': 'off', |
| 129 | + } |
| 130 | + }) |
| 131 | +] |
| 132 | +``` |
0 commit comments