cypress-plugin-last-failed 1.0.1 → 1.1.0

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.
package/CONTRIBUTING.md CHANGED
@@ -30,7 +30,7 @@ Thanks for being willing to contribute!
30
30
  Please make sure to run the node script before you commit your changes:
31
31
 
32
32
  ```bash
33
- npx cypress-last-failed run
33
+ npx cypress-plugin-last-failed run
34
34
  ```
35
35
 
36
36
  For changes related to the `cypress open` toggle, you can run `npx cypress open` to test functionality in the Cypress Test Runner UI. Make sure to include any test changes (if they exist) in your commit.
package/README.md CHANGED
@@ -24,6 +24,7 @@ A companion Cypress plugin for <code>cy-grep</code> that re-runs the last failed
24
24
  - [Setting up a `npm` script](#-setting-up-a-npm-script)
25
25
  - [Open mode](#-open-mode)
26
26
  - [Recommended open mode env variables](#recommended-open-mode-env-variables)
27
+ - [Use Required Test Tags Instead Of Skipping Tests](#use-required-test-tags-instead-of-skipping-tests)
27
28
  - [CI support](#continuous-integration-support)
28
29
  - [Typescript support](#typescript-support)
29
30
  - [Contributions](#contributions)
@@ -151,6 +152,29 @@ Toggling the filter will run any previously failed tests on the particular spec
151
152
  > [!NOTE]
152
153
  > More information on `grepOmitFiltered` and `grepFilterSpecs` can be read within the [README for `@bahmutov/cy-grep`](https://github.com/bahmutov/cy-grep?tab=readme-ov-file#pre-filter-specs-grepfilterspecs).
153
154
 
155
+ ### Use Required Test Tags Instead Of Skipping Tests
156
+
157
+ > [!NOTE]
158
+ > Read more about this topic within a blog post [Use Required Test Tags Instead Of Skipping Tests](https://glebbahmutov.com/blog/required-tags-instead-of-skipped-tests/) and within the [README for `@bahmutov/cy-grep`](https://github.com/bahmutov/cy-grep#required-tags).
159
+
160
+ Normally, any Cypress test or suite of tests marked with a `.skip` will be shown when running tests or within the Cypress test runner UI.
161
+
162
+ Since this plugin uses `@bahmutov/cy-grep` plugin, we can instead designate skipped tests using a **required tag**:
163
+
164
+ ```js
165
+ it('deletes an item', { requiredTags: '@skip' }, () => {
166
+ expect(1).to.equal(2);
167
+ });
168
+ ```
169
+
170
+ Now running or opening Cypress in interactive mode, **you will not see any tests with `requiredTags` including `@skip`** (unless setting environment variable `grepTags=@skip`).
171
+
172
+ To run just those tests with the required tag `@skip` in interactive mode:
173
+
174
+ ```bash
175
+ npx cypress open --env grepTags=@skip
176
+ ```
177
+
154
178
  ---
155
179
 
156
180
  ## Continuous integration support
@@ -6,9 +6,11 @@ describe('Should run expected tests', () => {
6
6
  expect(true).to.eq(false);
7
7
  }
8
8
  });
9
+
9
10
  it('should not run', () => {
10
11
  expect(true).to.eq(true);
11
12
  });
13
+
12
14
  it('needs to run', () => {
13
15
  if (Cypress.env('shouldPass')) {
14
16
  expect(1).to.eq(1);
@@ -16,6 +18,7 @@ describe('Should run expected tests', () => {
16
18
  expect(1).to.eq(2);
17
19
  }
18
20
  });
21
+
19
22
  it('will be included in failed tests', () => {
20
23
  if (Cypress.env('shouldPass')) {
21
24
  expect(10).to.eq(10);
@@ -23,4 +26,16 @@ describe('Should run expected tests', () => {
23
26
  expect(10).to.eq(2);
24
27
  }
25
28
  });
29
+
30
+ it('retry', { requiredTags: '@skip', retries: 1 }, () => {
31
+ if (Cypress.currentRetry === 0) {
32
+ expect(10).to.eq(2);
33
+ } else {
34
+ expect(10).to.eq(10);
35
+ }
36
+ });
37
+
38
+ it('skipped', { requiredTags: '@skip' }, () => {
39
+ expect(10).to.eq(10);
40
+ });
26
41
  });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "cypress-plugin-last-failed",
3
- "version": "1.0.1",
3
+ "version": "1.1.0",
4
4
  "description": "Cypress plugin to rerun last failed tests in cypress run and open mode",
5
5
  "main": "./src/index.js",
6
6
  "scripts": {
package/src/index.js CHANGED
@@ -1,6 +1,6 @@
1
1
  const fs = require('fs');
2
2
  const path = require('path');
3
-
3
+ const failedTestToggle = require('./toggle');
4
4
  /**
5
5
  * Collects failed tests from the most recent Cypress test run
6
6
  *
@@ -52,143 +52,4 @@ const collectFailingTests = (on, config) => {
52
52
  return collectFailingTests;
53
53
  };
54
54
 
55
- /**
56
- * Toggle for use within a spec file during `cypress open`
57
- */
58
-
59
- const failedTestToggle = () => {
60
- const hasStyles = top?.document.querySelector('#runFailedStyle');
61
- const hasToggleButton = top?.document.querySelector('#runFailedToggle');
62
- const defaultStyles = `
63
- .reporter header {
64
- overflow: visible;
65
- z-index: 2;
66
- }
67
- #runFailedControls {
68
- position: relative;
69
- display: inline-block;
70
- }
71
- #runFailedToggle {
72
- display: none;
73
- }
74
- #runFailedControls label {
75
- background-color: transparent;
76
- padding-top: 5px;
77
- }
78
- #runFailedControls #runFailedTooltip {
79
- visibility: hidden;
80
- width: 134px;
81
- background-color: #f3f4fa;
82
- color: #1b1e2e;
83
- text-align: center;
84
- padding: 5px;
85
- border-radius: 3px;
86
- position: absolute;
87
- z-index: 1;
88
- top: 27px;
89
- left: 0px;
90
- height: 28px;
91
- }
92
- #runFailedControls:hover #runFailedTooltip {
93
- visibility: visible;
94
- }
95
- #runFailedButton #runFailedLabel {
96
- cursor: pointer;
97
- }
98
- #runFailedTooltip::after {
99
- content: " ";
100
- position: absolute;
101
- bottom: 100%; /* At the top of the tooltip */
102
- right: 85%;
103
- margin-left: -5px;
104
- border-width: 5px;
105
- border-style: solid;
106
- border-color: transparent transparent #f3f4fa transparent;
107
- }
108
- .reporter:has(#runFailed:checked) .command.command-name-request:has(.command-is-event) {
109
- display:none
110
- }
111
- `;
112
- const turnOffRunFailedIcon = `<svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" fill="#f59aa9" class="bi bi-filter-circle" viewBox="0 0 16 16">
113
- <path d="M8 15A7 7 0 1 1 8 1a7 7 0 0 1 0 14m0 1A8 8 0 1 0 8 0a8 8 0 0 0 0 16"/>
114
- <path d="M7 11.5a.5.5 0 0 1 .5-.5h1a.5.5 0 0 1 0 1h-1a.5.5 0 0 1-.5-.5m-2-3a.5.5 0 0 1 .5-.5h5a.5.5 0 0 1 0 1h-5a.5.5 0 0 1-.5-.5m-2-3a.5.5 0 0 1 .5-.5h9a.5.5 0 0 1 0 1h-9a.5.5 0 0 1-.5-.5"/>
115
- </svg>`;
116
-
117
- const turnOnRunFailedIcon = `<svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" fill="#f59aa9" class="bi bi-filter-circle-fill" viewBox="0 0 16 16">
118
- <path d="M8 16A8 8 0 1 0 8 0a8 8 0 0 0 0 16M3.5 5h9a.5.5 0 0 1 0 1h-9a.5.5 0 0 1 0-1M5 8.5a.5.5 0 0 1 .5-.5h5a.5.5 0 0 1 0 1h-5a.5.5 0 0 1-.5-.5m2 3a.5.5 0 0 1 .5-.5h1a.5.5 0 0 1 0 1h-1a.5.5 0 0 1-.5-.5"/>
119
- </svg>`;
120
-
121
- const turnOffRunFailedDescription = 'Filter failed tests';
122
- const turnOnRunFailedDescription = 'Unfilter failed tests';
123
-
124
- // append styles
125
- if (!hasStyles) {
126
- const reporterEl = top?.document.querySelector('#unified-reporter');
127
- const reporterStyleEl = document.createElement('style');
128
- reporterStyleEl.setAttribute('id', 'runFailedStyle');
129
- reporterStyleEl.innerHTML = defaultStyles;
130
- reporterEl?.appendChild(reporterStyleEl);
131
- }
132
-
133
- if (!hasToggleButton) {
134
- const header = top?.document.querySelector('#unified-reporter header');
135
- const headerToggleDiv = document.createElement('div');
136
- const headerToggleSpan = document.createElement('span');
137
- const headerToggleTooltip = document.createElement('span');
138
- const headerToggleButton = document.createElement('button');
139
- const headerToggleInput = document.createElement('input');
140
- const headerToggleLabel = document.createElement('label');
141
-
142
- headerToggleInput.setAttribute('type', 'checkbox');
143
-
144
- headerToggleInput.setAttribute('id', 'runFailedToggle');
145
- headerToggleLabel.setAttribute('for', 'runFailedToggle');
146
- headerToggleLabel.setAttribute('id', 'runFailedLabel');
147
- headerToggleLabel.innerHTML = turnOffRunFailedIcon;
148
-
149
- headerToggleDiv.setAttribute('class', 'controls');
150
- headerToggleDiv.setAttribute('id', 'runFailedControls');
151
- headerToggleTooltip.setAttribute('id', 'runFailedTooltip');
152
- headerToggleTooltip.innerText = turnOffRunFailedDescription;
153
- headerToggleButton.setAttribute('aria-label', turnOffRunFailedDescription);
154
- headerToggleButton.setAttribute('id', 'runFailedButton');
155
-
156
- header?.appendChild(headerToggleDiv);
157
- headerToggleDiv?.appendChild(headerToggleSpan);
158
- headerToggleDiv?.appendChild(headerToggleTooltip);
159
- headerToggleSpan?.appendChild(headerToggleButton);
160
- headerToggleButton?.appendChild(headerToggleInput);
161
- headerToggleButton?.appendChild(headerToggleLabel);
162
- }
163
-
164
- const runFailedElement = top.document.querySelector('#runFailedToggle');
165
- const runFailedLabelElement = top.document.querySelector(
166
- '[for=runFailedToggle]'
167
- );
168
- const runFailedTooltipElement =
169
- top.document.querySelector('#runFailedTooltip');
170
-
171
- runFailedElement?.addEventListener('change', (e) => {
172
- const stopBtn = window.top.document.querySelector('.reporter .stop');
173
-
174
- if (e.target.checked) {
175
- if (stopBtn) {
176
- stopBtn.click();
177
- }
178
- // when checked, grep only failed tests in spec
179
- Cypress.grepFailed();
180
- runFailedLabelElement.innerHTML = turnOnRunFailedIcon;
181
- runFailedTooltipElement.innerHTML = turnOnRunFailedDescription;
182
- } else {
183
- if (stopBtn) {
184
- stopBtn.click();
185
- }
186
- // when unchecked, ungrep and show all tests in spec
187
- Cypress.grep();
188
- runFailedLabelElement.innerHTML = turnOffRunFailedIcon;
189
- runFailedTooltipElement.innerHTML = turnOffRunFailedDescription;
190
- }
191
- });
192
- };
193
-
194
55
  module.exports = { collectFailingTests, failedTestToggle };
package/src/toggle.js ADDED
@@ -0,0 +1,195 @@
1
+ /**
2
+ * Find and grep all the failed test titles designated within the Cypress Test Runner UI.
3
+ *
4
+ * Any retried tests that failed but ultimately passed will not be included.
5
+ *
6
+ * See README for recommendation on handling skipped tests ordinarily seen within the Cypress Test Runner UI.
7
+ */
8
+
9
+ const grepFailed = () => {
10
+ // @ts-ignore
11
+ const failedTestTitles = [];
12
+
13
+ const failedTests = window.top?.document.querySelectorAll(
14
+ '.test.runnable.runnable-failed'
15
+ );
16
+
17
+ [...failedTests].forEach((test) => {
18
+ failedTestTitles.push(test.innerText.split('\n')[0]);
19
+ });
20
+
21
+ if (!failedTestTitles.length) {
22
+ console.log('No failed tests found');
23
+ } else {
24
+ console.log('running only the failed tests');
25
+ const grepTitles = failedTestTitles.join('; ');
26
+ // @ts-ignore
27
+ Cypress.grep(grepTitles);
28
+ }
29
+ };
30
+
31
+ /**
32
+ * Toggle for use within a spec file during `cypress open`
33
+ */
34
+
35
+ const failedTestToggle = () => {
36
+ const hasStyles = top?.document.querySelector('#runFailedStyle');
37
+ const hasToggleButton = top?.document.querySelector('#runFailedToggle');
38
+ const defaultStyles = `
39
+ .reporter header {
40
+ overflow: visible;
41
+ z-index: 2;
42
+ }
43
+ #runFailedControls {
44
+ position: relative;
45
+ display: inline-block;
46
+ }
47
+ #runFailedToggle {
48
+ display: none;
49
+ }
50
+ #runFailedControls label {
51
+ background-color: transparent;
52
+ padding-top: 5px;
53
+ }
54
+ #runFailedControls #runFailedTooltip {
55
+ visibility: hidden;
56
+ width: 134px;
57
+ background-color: #f3f4fa;
58
+ color: #1b1e2e;
59
+ text-align: center;
60
+ padding: 5px;
61
+ border-radius: 3px;
62
+ position: absolute;
63
+ z-index: 1;
64
+ top: 27px;
65
+ left: 0px;
66
+ height: 28px;
67
+ }
68
+ #runFailedControls:hover #runFailedTooltip {
69
+ visibility: visible;
70
+ }
71
+ #runFailedButton #runFailedLabel {
72
+ cursor: pointer;
73
+ }
74
+ #runFailedTooltip::after {
75
+ content: " ";
76
+ position: absolute;
77
+ bottom: 100%; /* At the top of the tooltip */
78
+ right: 85%;
79
+ margin-left: -5px;
80
+ border-width: 5px;
81
+ border-style: solid;
82
+ border-color: transparent transparent #f3f4fa transparent;
83
+ }
84
+ .reporter:has(#runFailed:checked) .command.command-name-request:has(.command-is-event) {
85
+ display:none
86
+ }
87
+ `;
88
+ const turnOffRunFailedIcon = `<svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" fill="#f59aa9" class="bi bi-filter-circle" viewBox="0 0 16 16">
89
+ <path d="M8 15A7 7 0 1 1 8 1a7 7 0 0 1 0 14m0 1A8 8 0 1 0 8 0a8 8 0 0 0 0 16"/>
90
+ <path d="M7 11.5a.5.5 0 0 1 .5-.5h1a.5.5 0 0 1 0 1h-1a.5.5 0 0 1-.5-.5m-2-3a.5.5 0 0 1 .5-.5h5a.5.5 0 0 1 0 1h-5a.5.5 0 0 1-.5-.5m-2-3a.5.5 0 0 1 .5-.5h9a.5.5 0 0 1 0 1h-9a.5.5 0 0 1-.5-.5"/>
91
+ </svg>`;
92
+
93
+ const turnOnRunFailedIcon = `<svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" fill="#f59aa9" class="bi bi-filter-circle-fill" viewBox="0 0 16 16">
94
+ <path d="M8 16A8 8 0 1 0 8 0a8 8 0 0 0 0 16M3.5 5h9a.5.5 0 0 1 0 1h-9a.5.5 0 0 1 0-1M5 8.5a.5.5 0 0 1 .5-.5h5a.5.5 0 0 1 0 1h-5a.5.5 0 0 1-.5-.5m2 3a.5.5 0 0 1 .5-.5h1a.5.5 0 0 1 0 1h-1a.5.5 0 0 1-.5-.5"/>
95
+ </svg>`;
96
+
97
+ const turnOffRunFailedDescription = 'Filter failed tests';
98
+ const turnOnRunFailedDescription = 'Unfilter failed tests';
99
+
100
+ // append styles
101
+ if (!hasStyles) {
102
+ const reporterEl = top?.document.querySelector('#unified-reporter');
103
+ const reporterStyleEl = document.createElement('style');
104
+ reporterStyleEl.setAttribute('id', 'runFailedStyle');
105
+ reporterStyleEl.innerHTML = defaultStyles;
106
+ reporterEl?.appendChild(reporterStyleEl);
107
+ }
108
+
109
+ if (!hasToggleButton) {
110
+ const header = top?.document.querySelector('#unified-reporter header');
111
+ const headerToggleDiv = document.createElement('div');
112
+ const headerToggleSpan = document.createElement('span');
113
+ const headerToggleTooltip = document.createElement('span');
114
+ const headerToggleButton = document.createElement('button');
115
+ const headerToggleInput = document.createElement('input');
116
+ const headerToggleLabel = document.createElement('label');
117
+
118
+ headerToggleInput.setAttribute('type', 'checkbox');
119
+
120
+ headerToggleInput.setAttribute('id', 'runFailedToggle');
121
+ headerToggleLabel.setAttribute('for', 'runFailedToggle');
122
+ headerToggleLabel.setAttribute('id', 'runFailedLabel');
123
+ headerToggleLabel.innerHTML = turnOffRunFailedIcon;
124
+
125
+ headerToggleDiv.setAttribute('class', 'controls');
126
+ headerToggleDiv.setAttribute('id', 'runFailedControls');
127
+ headerToggleTooltip.setAttribute('id', 'runFailedTooltip');
128
+ headerToggleTooltip.innerText = turnOffRunFailedDescription;
129
+ headerToggleButton.setAttribute('aria-label', turnOffRunFailedDescription);
130
+ headerToggleButton.setAttribute('id', 'runFailedButton');
131
+
132
+ header?.appendChild(headerToggleDiv);
133
+ headerToggleDiv?.appendChild(headerToggleSpan);
134
+ headerToggleDiv?.appendChild(headerToggleTooltip);
135
+ headerToggleSpan?.appendChild(headerToggleButton);
136
+ headerToggleButton?.appendChild(headerToggleInput);
137
+ headerToggleButton?.appendChild(headerToggleLabel);
138
+ }
139
+
140
+ const runFailedElement = top.document.querySelector('#runFailedToggle');
141
+ const runFailedLabelElement = top.document.querySelector(
142
+ '[for=runFailedToggle]'
143
+ );
144
+ const runFailedTooltipElement =
145
+ top.document.querySelector('#runFailedTooltip');
146
+
147
+ runFailedElement?.addEventListener('change', (e) => {
148
+ const stopBtn = window.top.document.querySelector('.reporter .stop');
149
+
150
+ if (e.target.checked) {
151
+ if (stopBtn) {
152
+ stopBtn.click();
153
+ }
154
+ // when checked, grep only failed tests in spec
155
+ grepFailed();
156
+
157
+ runFailedLabelElement.innerHTML = turnOnRunFailedIcon;
158
+ runFailedTooltipElement.innerHTML = turnOnRunFailedDescription;
159
+ } else {
160
+ if (stopBtn) {
161
+ stopBtn.click();
162
+ }
163
+ // when unchecked, ungrep and show all tests in spec
164
+ Cypress.grep();
165
+ runFailedLabelElement.innerHTML = turnOffRunFailedIcon;
166
+ runFailedTooltipElement.innerHTML = turnOffRunFailedDescription;
167
+ }
168
+ });
169
+ // Wrapping logic within isInteractive check
170
+ // This targets cypress open mode where user can switch specs
171
+ if (Cypress.config('isInteractive')) {
172
+ Cypress.on('window:unload', () => {
173
+ // Store the current Cypress test runner url
174
+ // This is to check against any spec change in test runner while the grep filter is activated
175
+ // If a user does switch spec while filter is active, the filter will be reset
176
+ const sidebarRunsLinkPage = window.top?.document.querySelector(
177
+ '[data-cy="sidebar-link-runs-page"]'
178
+ );
179
+ const runFailedToggleElement =
180
+ window.top?.document.querySelector('#runFailedToggle');
181
+
182
+ if (
183
+ window.top?.document.URL !=
184
+ sidebarRunsLinkPage.getAttribute('data-url') &&
185
+ runFailedToggleElement.checked
186
+ ) {
187
+ runFailedToggleElement.click();
188
+ }
189
+
190
+ sidebarRunsLinkPage.setAttribute('data-url', window.top?.document.URL);
191
+ });
192
+ }
193
+ };
194
+
195
+ module.exports = failedTestToggle;