cypress-plugin-last-failed 1.0.0 → 1.0.2
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/.github/workflows/main.yml +1 -1
- package/CONTRIBUTING.md +1 -1
- package/README.md +32 -24
- package/assets/cypress-last-failed.png +0 -0
- package/cypress/e2e/tests.cy.js +15 -0
- package/cypress.config.js +0 -1
- package/package.json +3 -3
- package/runFailed.js +5 -10
- package/src/index.js +39 -11
|
@@ -24,5 +24,5 @@ jobs:
|
|
|
24
24
|
uses: cypress-io/github-action@v6
|
|
25
25
|
with:
|
|
26
26
|
# environment variable used for CI/CD tests in this repo
|
|
27
|
-
command: npx cypress-last-failed run --env shouldPass=true
|
|
27
|
+
command: npx cypress-plugin-last-failed run --env shouldPass=true
|
|
28
28
|
working-directory: ${{ github.workspace }}
|
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
|
@@ -20,11 +20,11 @@ A companion Cypress plugin for <code>cy-grep</code> that re-runs the last failed
|
|
|
20
20
|
|
|
21
21
|
- [Installation](#-installation)
|
|
22
22
|
- [Run mode](#-run-mode)
|
|
23
|
-
- [Optional custom `failedTestDirectory`](#optional-custom-failedtestdirectory)
|
|
24
23
|
- [Add rule to gitignore](#add-rule-to-gitignore)
|
|
25
24
|
- [Setting up a `npm` script](#-setting-up-a-npm-script)
|
|
26
25
|
- [Open mode](#-open-mode)
|
|
27
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)
|
|
28
28
|
- [CI support](#continuous-integration-support)
|
|
29
29
|
- [Typescript support](#typescript-support)
|
|
30
30
|
- [Contributions](#contributions)
|
|
@@ -54,10 +54,12 @@ failedTestToggle();
|
|
|
54
54
|
3. In `cypress.config`, include the following within `setupNodeEvents` for `e2e` and/or `component` testing:
|
|
55
55
|
|
|
56
56
|
```js
|
|
57
|
+
const { defineConfig } = require('cypress');
|
|
58
|
+
const { collectFailingTests } = require('cypress-plugin-last-failed');
|
|
59
|
+
|
|
57
60
|
module.exports = defineConfig({
|
|
58
61
|
screenshotOnRunFailure: false,
|
|
59
62
|
env: {
|
|
60
|
-
failedTestDirectory: './',
|
|
61
63
|
grepOmitFiltered: true,
|
|
62
64
|
grepFilterSpecs: true,
|
|
63
65
|
},
|
|
@@ -94,34 +96,17 @@ npx cypress run
|
|
|
94
96
|
2. If there are failed tests, run the following command from the **directory of the project's `cypress.config`**:
|
|
95
97
|
|
|
96
98
|
```bash
|
|
97
|
-
npx cypress-last-failed run
|
|
99
|
+
npx cypress-plugin-last-failed run
|
|
98
100
|
```
|
|
99
101
|
|
|
100
102
|
You can also include more cli arguments similar to `cypress run`, as the command harnesses the power of [Cypress module API](https://docs.cypress.io/guides/guides/module-api):
|
|
101
103
|
|
|
102
104
|
```bash
|
|
103
105
|
# Example
|
|
104
|
-
npx cypress-last-failed run --e2e --browser chrome
|
|
106
|
+
npx cypress-plugin-last-failed run --e2e --browser chrome
|
|
105
107
|
```
|
|
106
108
|
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
By default, there will be a folder called `test-results` created in the directory of the `cypress.config`.
|
|
110
|
-
|
|
111
|
-
- To customize where the `test-results` folder should be stored, add the `failedTestDirectory` environment variable:
|
|
112
|
-
|
|
113
|
-
```js
|
|
114
|
-
// Example using a fixtures folder path relative to the cypress.config
|
|
115
|
-
|
|
116
|
-
module.exports = defineConfig({
|
|
117
|
-
env: {
|
|
118
|
-
failedTestDirectory: './cypress/fixtures',
|
|
119
|
-
},
|
|
120
|
-
e2e: {
|
|
121
|
-
setupNodeEvents(on, config) {},
|
|
122
|
-
},
|
|
123
|
-
});
|
|
124
|
-
```
|
|
109
|
+
There will be a folder called `test-results` created in the directory of the `cypress.config`.
|
|
125
110
|
|
|
126
111
|
### Add rule to gitignore
|
|
127
112
|
|
|
@@ -141,7 +126,7 @@ For convenience, you may desire to house the `npx` command within an npm script
|
|
|
141
126
|
|
|
142
127
|
```json
|
|
143
128
|
"scripts": {
|
|
144
|
-
"last-failed": "npx cypress-
|
|
129
|
+
"last-failed": "npx cypress-plugin-last-failed run --e2e --browser electron"
|
|
145
130
|
}
|
|
146
131
|
```
|
|
147
132
|
|
|
@@ -167,6 +152,29 @@ Toggling the filter will run any previously failed tests on the particular spec
|
|
|
167
152
|
> [!NOTE]
|
|
168
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).
|
|
169
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
|
+
|
|
170
178
|
---
|
|
171
179
|
|
|
172
180
|
## Continuous integration support
|
|
@@ -198,7 +206,7 @@ jobs:
|
|
|
198
206
|
if: always()
|
|
199
207
|
uses: cypress-io/github-action@v6
|
|
200
208
|
with:
|
|
201
|
-
command: npx cypress-last-failed run
|
|
209
|
+
command: npx cypress-plugin-last-failed run
|
|
202
210
|
working-directory: ${{ github.workspace }}
|
|
203
211
|
```
|
|
204
212
|
|
|
Binary file
|
package/cypress/e2e/tests.cy.js
CHANGED
|
@@ -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/cypress.config.js
CHANGED
package/package.json
CHANGED
|
@@ -1,13 +1,13 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "cypress-plugin-last-failed",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.2",
|
|
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": {
|
|
7
|
-
"last-failed": "
|
|
7
|
+
"last-failed": "npx cypress-plugin-last-failed run --e2e --browser electron"
|
|
8
8
|
},
|
|
9
9
|
"bin": {
|
|
10
|
-
"cypress-last-failed": "runFailed.js"
|
|
10
|
+
"cypress-plugin-last-failed": "runFailed.js"
|
|
11
11
|
},
|
|
12
12
|
"keywords": [
|
|
13
13
|
"cypress",
|
package/runFailed.js
CHANGED
|
@@ -4,18 +4,13 @@ const cypress = require('cypress');
|
|
|
4
4
|
const fs = require('fs');
|
|
5
5
|
const path = require('path');
|
|
6
6
|
const appDir = process.env.INIT_CWD ? process.env.INIT_CWD : path.resolve('.');
|
|
7
|
-
const cypressConfig = require(`${appDir}/cypress.config`);
|
|
8
7
|
|
|
9
8
|
async function runLastFailed() {
|
|
10
|
-
const noFailedTestsMessage =
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
const failedTestFilePath =
|
|
16
|
-
cypressConfig.env?.failedTestDirectory === undefined
|
|
17
|
-
? `${appDir}/test-results/last-run.txt`
|
|
18
|
-
: `${cypressConfig.env.failedTestDirectory}/test-results/last-run.txt`;
|
|
9
|
+
const noFailedTestsMessage = `No previous failed tests detected
|
|
10
|
+
Ensure you are in the directory of your cypress config
|
|
11
|
+
Try running tests again with cypress run`;
|
|
12
|
+
|
|
13
|
+
const failedTestFilePath = `${appDir}/test-results/last-run.txt`;
|
|
19
14
|
|
|
20
15
|
if (fs.existsSync(failedTestFilePath)) {
|
|
21
16
|
// Retrieve the failedTests from the file
|
package/src/index.js
CHANGED
|
@@ -6,9 +6,7 @@ const path = require('path');
|
|
|
6
6
|
*
|
|
7
7
|
* After each run, a file will store failed test titles within a test-results directory
|
|
8
8
|
*
|
|
9
|
-
*
|
|
10
|
-
*
|
|
11
|
-
* If failedTestDirectory env var is unset, test-results will be stored in cypress.config directory
|
|
9
|
+
* The test-results directory will be stored in cypress.config directory
|
|
12
10
|
*
|
|
13
11
|
* Subsequent test runs containing failed tests will overwrite this file
|
|
14
12
|
* @param {*} on
|
|
@@ -17,7 +15,6 @@ const path = require('path');
|
|
|
17
15
|
*/
|
|
18
16
|
|
|
19
17
|
const collectFailingTests = (on, config) => {
|
|
20
|
-
// Check for environment variable `collectFailingTests` to be true
|
|
21
18
|
on('after:run', async (results) => {
|
|
22
19
|
let failedTests = [];
|
|
23
20
|
// Grab every failed test's title
|
|
@@ -36,13 +33,12 @@ const collectFailingTests = (on, config) => {
|
|
|
36
33
|
// Prepare a string that can be read from cy-grep
|
|
37
34
|
const greppedTestFormat = stringedTests.replaceAll(',', '; ');
|
|
38
35
|
|
|
39
|
-
// Use the cypress.config
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
? `${path.dirname(config.configFile)}/test-results/`
|
|
44
|
-
: `${config.env.failedTestDirectory}/test-results/`;
|
|
36
|
+
// Use the cypress.config directory for path for storing test-results
|
|
37
|
+
const failedTestFileDirectory = `${path.dirname(
|
|
38
|
+
config.configFile
|
|
39
|
+
)}/test-results/`;
|
|
45
40
|
|
|
41
|
+
// Create the directory and last-run file where failed tests will be written to
|
|
46
42
|
await fs.promises.mkdir(`${failedTestFileDirectory}`, {
|
|
47
43
|
recursive: true,
|
|
48
44
|
});
|
|
@@ -56,6 +52,37 @@ const collectFailingTests = (on, config) => {
|
|
|
56
52
|
return collectFailingTests;
|
|
57
53
|
};
|
|
58
54
|
|
|
55
|
+
/**
|
|
56
|
+
* Find and grep all the failed test titles designated within the Cypress Test Runner UI.
|
|
57
|
+
*
|
|
58
|
+
* Any retried tests that failed but ultimately passed will not be included.
|
|
59
|
+
*
|
|
60
|
+
* See README for recommendation on handling skipped tests ordinarily seen within the Cypress Test Runner UI.
|
|
61
|
+
*/
|
|
62
|
+
|
|
63
|
+
const grepFailed = () => {
|
|
64
|
+
// @ts-ignore
|
|
65
|
+
const failedTestTitles = [];
|
|
66
|
+
|
|
67
|
+
const failedTests = window.top?.document.querySelectorAll(
|
|
68
|
+
'.test.runnable.runnable-failed'
|
|
69
|
+
);
|
|
70
|
+
|
|
71
|
+
[...failedTests].forEach((test) => {
|
|
72
|
+
failedTestTitles.push(test.innerText.split('\n')[0]);
|
|
73
|
+
});
|
|
74
|
+
|
|
75
|
+
if (!failedTestTitles.length) {
|
|
76
|
+
console.log('No failed tests found');
|
|
77
|
+
} else {
|
|
78
|
+
console.log('running only the failed tests');
|
|
79
|
+
const grepTitles = failedTestTitles.join('; ');
|
|
80
|
+
console.log(grepTitles);
|
|
81
|
+
// @ts-ignore
|
|
82
|
+
Cypress.grep(grepTitles);
|
|
83
|
+
}
|
|
84
|
+
};
|
|
85
|
+
|
|
59
86
|
/**
|
|
60
87
|
* Toggle for use within a spec file during `cypress open`
|
|
61
88
|
*/
|
|
@@ -180,7 +207,8 @@ const failedTestToggle = () => {
|
|
|
180
207
|
stopBtn.click();
|
|
181
208
|
}
|
|
182
209
|
// when checked, grep only failed tests in spec
|
|
183
|
-
|
|
210
|
+
grepFailed();
|
|
211
|
+
|
|
184
212
|
runFailedLabelElement.innerHTML = turnOnRunFailedIcon;
|
|
185
213
|
runFailedTooltipElement.innerHTML = turnOnRunFailedDescription;
|
|
186
214
|
} else {
|