eslint-plugin-cypress 4.1.0 → 4.2.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (41) hide show
  1. package/.node-version +1 -1
  2. package/.nvmrc +1 -1
  3. package/README.md +1 -0
  4. package/circle.yml +82 -9
  5. package/docs/rules/no-xpath.md +25 -0
  6. package/eslint.config.mjs +1 -4
  7. package/legacy.js +1 -0
  8. package/lib/flat.js +1 -0
  9. package/lib/rules/no-xpath.js +35 -0
  10. package/package.json +6 -7
  11. package/test-project/README.md +48 -0
  12. package/test-project/cypress/e2e/1-getting-started/todo.cy.js +143 -0
  13. package/test-project/cypress/e2e/2-advanced-examples/actions.cy.js +321 -0
  14. package/test-project/cypress/e2e/2-advanced-examples/aliasing.cy.js +39 -0
  15. package/test-project/cypress/e2e/2-advanced-examples/assertions.cy.js +176 -0
  16. package/test-project/cypress/e2e/2-advanced-examples/connectors.cy.js +98 -0
  17. package/test-project/cypress/e2e/2-advanced-examples/cookies.cy.js +118 -0
  18. package/test-project/cypress/e2e/2-advanced-examples/cypress_api.cy.js +185 -0
  19. package/test-project/cypress/e2e/2-advanced-examples/files.cy.js +85 -0
  20. package/test-project/cypress/e2e/2-advanced-examples/location.cy.js +32 -0
  21. package/test-project/cypress/e2e/2-advanced-examples/misc.cy.js +90 -0
  22. package/test-project/cypress/e2e/2-advanced-examples/navigation.cy.js +55 -0
  23. package/test-project/cypress/e2e/2-advanced-examples/network_requests.cy.js +163 -0
  24. package/test-project/cypress/e2e/2-advanced-examples/querying.cy.js +114 -0
  25. package/test-project/cypress/e2e/2-advanced-examples/spies_stubs_clocks.cy.js +204 -0
  26. package/test-project/cypress/e2e/2-advanced-examples/storage.cy.js +117 -0
  27. package/test-project/cypress/e2e/2-advanced-examples/traversal.cy.js +121 -0
  28. package/test-project/cypress/e2e/2-advanced-examples/utilities.cy.js +107 -0
  29. package/test-project/cypress/e2e/2-advanced-examples/viewport.cy.js +58 -0
  30. package/test-project/cypress/e2e/2-advanced-examples/waiting.cy.js +30 -0
  31. package/test-project/cypress/e2e/2-advanced-examples/window.cy.js +22 -0
  32. package/test-project/cypress/fixtures/example.json +5 -0
  33. package/test-project/cypress/support/commands.js +25 -0
  34. package/test-project/cypress/support/e2e.js +17 -0
  35. package/test-project/cypress.config.js +9 -0
  36. package/test-project/eslint-configs/eslint.default.mjs +9 -0
  37. package/test-project/eslint-configs/eslint.globals.mjs +4 -0
  38. package/test-project/eslint-configs/eslint.recommended.mjs +9 -0
  39. package/test-project/eslint.config.mjs +9 -0
  40. package/test-project/package.json +9 -0
  41. package/tests/lib/rules/no-xpath.js +30 -0
package/.node-version CHANGED
@@ -1 +1 @@
1
- 20.12.2
1
+ 22.11.0
package/.nvmrc CHANGED
@@ -1 +1 @@
1
- 20.12.2
1
+ 22.11.0
package/README.md CHANGED
@@ -54,6 +54,7 @@ These rules enforce some of the [best practices recommended for using Cypress](h
54
54
  | [no-force](docs/rules/no-force.md) | disallow using `force: true` with action commands | |
55
55
  | [no-pause](docs/rules/no-pause.md) | disallow using `cy.pause()` calls | |
56
56
  | [no-unnecessary-waiting](docs/rules/no-unnecessary-waiting.md) | disallow waiting for arbitrary time periods | ✅ |
57
+ | [no-xpath](docs/rules/no-xpath.md) | disallow using `cy.xpath()` calls | |
57
58
  | [require-data-selectors](docs/rules/require-data-selectors.md) | require `data-*` attribute selectors | |
58
59
  | [unsafe-to-chain-command](docs/rules/unsafe-to-chain-command.md) | disallow actions within chains | ✅ |
59
60
 
package/circle.yml CHANGED
@@ -1,17 +1,37 @@
1
1
  version: 2.1
2
2
 
3
+ executors:
4
+ docker-executor:
5
+ docker:
6
+ - image: cimg/node:22.14.0
7
+ resource_class: medium
8
+
3
9
  workflows:
4
- version: 2.1
5
10
  main:
6
11
  jobs:
7
12
  - lint
8
13
  - test-v8
9
14
  - test-v9
15
+
16
+ - build-test-project
17
+ - test-test-project:
18
+ matrix:
19
+ parameters:
20
+ eslint-version: ['9']
21
+ config-file: [
22
+ # configurations correspond to examples in README
23
+ 'default',
24
+ 'recommended',
25
+ 'globals',
26
+ ]
27
+ requires:
28
+ - build-test-project
10
29
  - release:
11
30
  requires:
12
31
  - lint
13
32
  - test-v8
14
33
  - test-v9
34
+ - test-test-project
15
35
  filters:
16
36
  branches:
17
37
  only:
@@ -19,8 +39,7 @@ workflows:
19
39
 
20
40
  jobs:
21
41
  lint:
22
- docker:
23
- - image: cimg/node:22.11.0
42
+ executor: docker-executor
24
43
  steps:
25
44
  - checkout
26
45
  - run:
@@ -34,8 +53,7 @@ jobs:
34
53
  command: npm run lint
35
54
 
36
55
  test-v8:
37
- docker:
38
- - image: cimg/node:22.11.0
56
+ executor: docker-executor
39
57
  steps:
40
58
  - checkout
41
59
  - run:
@@ -52,8 +70,7 @@ jobs:
52
70
  command: npm run test:legacy
53
71
 
54
72
  test-v9:
55
- docker:
56
- - image: cimg/node:22.11.0
73
+ executor: docker-executor
57
74
  steps:
58
75
  - checkout
59
76
  - run:
@@ -69,9 +86,65 @@ jobs:
69
86
  name: Test ESLint 9
70
87
  command: npm test
71
88
 
89
+ build-test-project:
90
+ executor: docker-executor
91
+ steps:
92
+ - checkout
93
+ - run:
94
+ name: Install dependencies
95
+ command: npm ci
96
+ - run:
97
+ name: Build tarball
98
+ command: npm pack
99
+ - run:
100
+ name: Get version
101
+ command: |
102
+ echo "PLUGIN_VERSION=$(jq -r '.version' package.json)" >> $BASH_ENV
103
+ cp $BASH_ENV bash.env
104
+ - persist_to_workspace:
105
+ root: .
106
+ paths:
107
+ - eslint-plugin-cypress-*.tgz
108
+ - bash.env
109
+
110
+ test-test-project:
111
+ description: Run ESLint with different configurations
112
+ parameters:
113
+ eslint-version:
114
+ description: Version of ESLint to use
115
+ default: 'latest'
116
+ type: string
117
+ config-file:
118
+ description: Configuration file
119
+ default: 'default'
120
+ type: string
121
+ executor: docker-executor
122
+ working_directory: ./test-project
123
+ steps:
124
+ - checkout:
125
+ path: ../
126
+ - attach_workspace:
127
+ at: .
128
+ - run:
129
+ name: Get plugin version
130
+ command: |
131
+ cat bash.env >> $BASH_ENV
132
+ - run:
133
+ name: Install dependencies
134
+ command: |
135
+ npm install eslint@<< parameters.eslint-version>> ./eslint-plugin-cypress-$PLUGIN_VERSION.tgz -D
136
+ - run:
137
+ name: Display ESLint version
138
+ command: |
139
+ npx eslint --version
140
+ - run: echo Testing a << parameters.config-file >> configuration
141
+ - run:
142
+ name: Lint with example configuration
143
+ command: |
144
+ npx eslint --config ./eslint-configs/eslint.<< parameters.config-file >>.mjs .
145
+
72
146
  release:
73
- docker:
74
- - image: cimg/node:22.11.0
147
+ executor: docker-executor
75
148
  steps:
76
149
  - checkout
77
150
  - run:
@@ -0,0 +1,25 @@
1
+ # Disallow using `cy.xpath()` calls (`cypress/no-xpath`)
2
+
3
+ <!-- end auto-generated rule header -->
4
+
5
+ This rule disallows the usage of `cy.xpath()` for selecting elements.
6
+
7
+ ## Rule Details
8
+
9
+ Examples of **incorrect** code for this rule:
10
+
11
+ ```js
12
+ cy.xpath('//div[@class=\"container\"]').click()
13
+ ```
14
+
15
+ Examples of **correct** code for this rule:
16
+
17
+ ```js
18
+ cy.get('[data-cy="container"]').click();
19
+ ```
20
+
21
+ ## Further Reading
22
+
23
+ Both `@cypress/xpath` and `cypress-xpath` are deprecated.
24
+
25
+ See [the Cypress Best Practices guide](https://docs.cypress.io/guides/references/best-practices.html#Selecting-Elements).
package/eslint.config.mjs CHANGED
@@ -1,14 +1,13 @@
1
1
  import globals from 'globals'
2
2
  import pluginJs from '@eslint/js'
3
3
  import eslintPlugin from 'eslint-plugin-eslint-plugin'
4
- import nodePlugin from 'eslint-plugin-n'
5
4
  import mochaPlugin from 'eslint-plugin-mocha'
6
5
 
7
6
  export default [
8
7
  pluginJs.configs.recommended,
9
8
  eslintPlugin.configs['flat/recommended'],
10
- nodePlugin.configs['flat/recommended-script'],
11
9
  mochaPlugin.configs.flat.recommended,
10
+ {ignores: ['test-project/']},
12
11
  {
13
12
  languageOptions: {
14
13
  globals: globals.node
@@ -20,8 +19,6 @@ export default [
20
19
  'eslint-plugin/require-meta-docs-description': 'error',
21
20
  'eslint-plugin/meta-property-ordering': 'error',
22
21
  'eslint-plugin/test-case-property-ordering': 'error',
23
- 'n/no-extraneous-require':
24
- ['error', { 'allowModules': ['jest-config'] }],
25
22
  'mocha/no-mocha-arrows': 'off',
26
23
  'mocha/no-setup-in-describe': 'off'
27
24
  }
package/legacy.js CHANGED
@@ -12,6 +12,7 @@ module.exports = {
12
12
  'no-force': require('./lib/rules/no-force'),
13
13
  'no-pause': require('./lib/rules/no-pause'),
14
14
  'no-debug': require('./lib/rules/no-debug'),
15
+ 'no-xpath': require('./lib/rules/no-xpath'),
15
16
  },
16
17
  configs: {
17
18
  recommended: require('./lib/config/recommended'),
package/lib/flat.js CHANGED
@@ -15,6 +15,7 @@ const plugin = {
15
15
  'no-force': require('./rules/no-force'),
16
16
  'no-pause': require('./rules/no-pause'),
17
17
  'no-debug': require('./rules/no-debug'),
18
+ 'no-xpath': require('./rules/no-xpath'),
18
19
  },
19
20
  }
20
21
 
@@ -0,0 +1,35 @@
1
+ 'use strict'
2
+
3
+ module.exports = {
4
+ meta: {
5
+ type: 'suggestion',
6
+ docs: {
7
+ description: 'disallow using `cy.xpath()` calls',
8
+ recommended: false,
9
+ url: 'https://github.com/cypress-io/eslint-plugin-cypress/blob/master/docs/rules/no-xpath.md'
10
+ },
11
+ fixable: null, // Or `code` or `whitespace`
12
+ schema: [], // Add a schema if the rule has options
13
+ messages: {
14
+ unexpected: 'Avoid using cy.xpath command',
15
+ },
16
+ },
17
+
18
+ create (context) {
19
+ return {
20
+ CallExpression (node) {
21
+ if (isCallingCyXpath(node)) {
22
+ context.report({ node, messageId: 'unexpected' })
23
+ }
24
+ },
25
+ }
26
+ },
27
+ };
28
+
29
+ function isCallingCyXpath (node) {
30
+ return node.callee.type === 'MemberExpression' &&
31
+ node.callee.object.type === 'Identifier' &&
32
+ node.callee.object.name === 'cy' &&
33
+ node.callee.property.type === 'Identifier' &&
34
+ node.callee.property.name === 'xpath'
35
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "eslint-plugin-cypress",
3
- "version": "4.1.0",
3
+ "version": "4.2.1",
4
4
  "description": "An ESLint plugin for projects using Cypress",
5
5
  "main": "legacy.js",
6
6
  "exports": {
@@ -26,16 +26,15 @@
26
26
  "eslint": ">=9"
27
27
  },
28
28
  "dependencies": {
29
- "globals": "^15.11.0"
29
+ "globals": "^15.15.0"
30
30
  },
31
31
  "devDependencies": {
32
- "eslint": "^9.13.0",
33
- "eslint-plugin-eslint-plugin": "^6.3.1",
32
+ "eslint": "^9.24.0",
33
+ "eslint-plugin-eslint-plugin": "^6.4.0",
34
34
  "eslint-plugin-mocha": "^10.5.0",
35
- "eslint-plugin-n": "^17.11.1",
36
- "husky": "^9.1.6",
35
+ "husky": "^9.1.7",
37
36
  "jest": "^29.7.0",
38
- "semantic-release": "24.2.0"
37
+ "semantic-release": "24.2.3"
39
38
  },
40
39
  "scripts": {
41
40
  "lint": "eslint \"*.js\" \"**/**/*.js\"",
@@ -0,0 +1,48 @@
1
+ # Test-project
2
+
3
+ This test project was generated via the Cypress App.
4
+
5
+ This project can be updated to the latest Cypress default scaffolded E2E test specs by carrying out the following steps in the directory `/test-project`:
6
+
7
+ ```shell
8
+ rm -rf cypress cypress.config.js
9
+ npm install cypress@latest --no-package-lock
10
+ npx cypress open
11
+ ```
12
+
13
+ - Select "Continue" for "What's New in Cypress" if displayed
14
+ - Select "E2E Testing"
15
+ - Select "Continue" in "Configuration files"
16
+ - Select "Electron" browser
17
+ - Select "Start E2E Testing in Electron"
18
+ - Select "Scaffold example specs"
19
+ - Close all Cypress windows
20
+
21
+ Test that scaffolded specs run:
22
+
23
+ ```shell
24
+ npm test
25
+ ```
26
+
27
+ Remove Cypress from `package.json`:
28
+
29
+ ```shell
30
+ npm uninstall cypress --no-package-lock
31
+ ```
32
+
33
+ ## Tests
34
+
35
+ Tests are run via [circle.yml](../circle.yml).
36
+
37
+ To test the project locally:
38
+
39
+ ```shell
40
+ cd test-project
41
+ npm install eslint@latest eslint-plugin-cypress@latest cypress@latest -D
42
+ npx cypress run
43
+ npx eslint
44
+ ```
45
+
46
+ Do not commit the changes from installing the above dependencies.
47
+ The CircleCI pipeline dynamically installs the latest `eslint*` dependencies.
48
+ Cypress is not needed for the CircleCI tests.
@@ -0,0 +1,143 @@
1
+ /// <reference types="cypress" />
2
+
3
+ // Welcome to Cypress!
4
+ //
5
+ // This spec file contains a variety of sample tests
6
+ // for a todo list app that are designed to demonstrate
7
+ // the power of writing tests in Cypress.
8
+ //
9
+ // To learn more about how Cypress works and
10
+ // what makes it such an awesome testing tool,
11
+ // please read our getting started guide:
12
+ // https://on.cypress.io/introduction-to-cypress
13
+
14
+ describe('example to-do app', () => {
15
+ beforeEach(() => {
16
+ // Cypress starts out with a blank slate for each test
17
+ // so we must tell it to visit our website with the `cy.visit()` command.
18
+ // Since we want to visit the same URL at the start of all our tests,
19
+ // we include it in our beforeEach function so that it runs before each test
20
+ cy.visit('https://example.cypress.io/todo')
21
+ })
22
+
23
+ it('displays two todo items by default', () => {
24
+ // We use the `cy.get()` command to get all elements that match the selector.
25
+ // Then, we use `should` to assert that there are two matched items,
26
+ // which are the two default items.
27
+ cy.get('.todo-list li').should('have.length', 2)
28
+
29
+ // We can go even further and check that the default todos each contain
30
+ // the correct text. We use the `first` and `last` functions
31
+ // to get just the first and last matched elements individually,
32
+ // and then perform an assertion with `should`.
33
+ cy.get('.todo-list li').first().should('have.text', 'Pay electric bill')
34
+ cy.get('.todo-list li').last().should('have.text', 'Walk the dog')
35
+ })
36
+
37
+ it('can add new todo items', () => {
38
+ // We'll store our item text in a variable so we can reuse it
39
+ const newItem = 'Feed the cat'
40
+
41
+ // Let's get the input element and use the `type` command to
42
+ // input our new list item. After typing the content of our item,
43
+ // we need to type the enter key as well in order to submit the input.
44
+ // This input has a data-test attribute so we'll use that to select the
45
+ // element in accordance with best practices:
46
+ // https://on.cypress.io/selecting-elements
47
+ cy.get('[data-test=new-todo]').type(`${newItem}{enter}`)
48
+
49
+ // Now that we've typed our new item, let's check that it actually was added to the list.
50
+ // Since it's the newest item, it should exist as the last element in the list.
51
+ // In addition, with the two default items, we should have a total of 3 elements in the list.
52
+ // Since assertions yield the element that was asserted on,
53
+ // we can chain both of these assertions together into a single statement.
54
+ cy.get('.todo-list li')
55
+ .should('have.length', 3)
56
+ .last()
57
+ .should('have.text', newItem)
58
+ })
59
+
60
+ it('can check off an item as completed', () => {
61
+ // In addition to using the `get` command to get an element by selector,
62
+ // we can also use the `contains` command to get an element by its contents.
63
+ // However, this will yield the <label>, which is lowest-level element that contains the text.
64
+ // In order to check the item, we'll find the <input> element for this <label>
65
+ // by traversing up the dom to the parent element. From there, we can `find`
66
+ // the child checkbox <input> element and use the `check` command to check it.
67
+ cy.contains('Pay electric bill')
68
+ .parent()
69
+ .find('input[type=checkbox]')
70
+ .check()
71
+
72
+ // Now that we've checked the button, we can go ahead and make sure
73
+ // that the list element is now marked as completed.
74
+ // Again we'll use `contains` to find the <label> element and then use the `parents` command
75
+ // to traverse multiple levels up the dom until we find the corresponding <li> element.
76
+ // Once we get that element, we can assert that it has the completed class.
77
+ cy.contains('Pay electric bill')
78
+ .parents('li')
79
+ .should('have.class', 'completed')
80
+ })
81
+
82
+ context('with a checked task', () => {
83
+ beforeEach(() => {
84
+ // We'll take the command we used above to check off an element
85
+ // Since we want to perform multiple tests that start with checking
86
+ // one element, we put it in the beforeEach hook
87
+ // so that it runs at the start of every test.
88
+ cy.contains('Pay electric bill')
89
+ .parent()
90
+ .find('input[type=checkbox]')
91
+ .check()
92
+ })
93
+
94
+ it('can filter for uncompleted tasks', () => {
95
+ // We'll click on the "active" button in order to
96
+ // display only incomplete items
97
+ cy.contains('Active').click()
98
+
99
+ // After filtering, we can assert that there is only the one
100
+ // incomplete item in the list.
101
+ cy.get('.todo-list li')
102
+ .should('have.length', 1)
103
+ .first()
104
+ .should('have.text', 'Walk the dog')
105
+
106
+ // For good measure, let's also assert that the task we checked off
107
+ // does not exist on the page.
108
+ cy.contains('Pay electric bill').should('not.exist')
109
+ })
110
+
111
+ it('can filter for completed tasks', () => {
112
+ // We can perform similar steps as the test above to ensure
113
+ // that only completed tasks are shown
114
+ cy.contains('Completed').click()
115
+
116
+ cy.get('.todo-list li')
117
+ .should('have.length', 1)
118
+ .first()
119
+ .should('have.text', 'Pay electric bill')
120
+
121
+ cy.contains('Walk the dog').should('not.exist')
122
+ })
123
+
124
+ it('can delete all completed tasks', () => {
125
+ // First, let's click the "Clear completed" button
126
+ // `contains` is actually serving two purposes here.
127
+ // First, it's ensuring that the button exists within the dom.
128
+ // This button only appears when at least one task is checked
129
+ // so this command is implicitly verifying that it does exist.
130
+ // Second, it selects the button so we can click it.
131
+ cy.contains('Clear completed').click()
132
+
133
+ // Then we can make sure that there is only one element
134
+ // in the list and our element does not exist
135
+ cy.get('.todo-list li')
136
+ .should('have.length', 1)
137
+ .should('not.have.text', 'Pay electric bill')
138
+
139
+ // Finally, make sure that the clear button no longer exists.
140
+ cy.contains('Clear completed').should('not.exist')
141
+ })
142
+ })
143
+ })