Skip to content

Commit c4fbe54

Browse files
Merge pull request #2 from dennisbergevin/dennis/fix-failedtestdir
fix: Refactoring logic for `failedTestDirectory` and updating README
2 parents 1580a9d + e9869cd commit c4fbe54

File tree

8 files changed

+26
-52
lines changed

8 files changed

+26
-52
lines changed

Diff for: .github/workflows/main.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -24,5 +24,5 @@ jobs:
2424
uses: cypress-io/github-action@v6
2525
with:
2626
# 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
2828
working-directory: ${{ github.workspace }}

Diff for: README.md

+8-24
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ A companion Cypress plugin for <code>cy-grep</code> that re-runs the last failed
2020

2121
- [Installation](#-installation)
2222
- [Run mode](#-run-mode)
23-
- [Optional custom `failedTestDirectory`](#optional-custom-failedtestdirectory)
2423
- [Add rule to gitignore](#add-rule-to-gitignore)
2524
- [Setting up a `npm` script](#-setting-up-a-npm-script)
2625
- [Open mode](#-open-mode)
@@ -54,10 +53,12 @@ failedTestToggle();
5453
3. In `cypress.config`, include the following within `setupNodeEvents` for `e2e` and/or `component` testing:
5554

5655
```js
56+
const { defineConfig } = require('cypress');
57+
const { collectFailingTests } = require('cypress-plugin-last-failed');
58+
5759
module.exports = defineConfig({
5860
screenshotOnRunFailure: false,
5961
env: {
60-
failedTestDirectory: './',
6162
grepOmitFiltered: true,
6263
grepFilterSpecs: true,
6364
},
@@ -94,34 +95,17 @@ npx cypress run
9495
2. If there are failed tests, run the following command from the **directory of the project's `cypress.config`**:
9596

9697
```bash
97-
npx cypress-last-failed run
98+
npx cypress-plugin-last-failed run
9899
```
99100

100101
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):
101102

102103
```bash
103104
# Example
104-
npx cypress-last-failed run --e2e --browser chrome
105+
npx cypress-plugin-last-failed run --e2e --browser chrome
105106
```
106107

107-
### Optional custom `failedTestDirectory`
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-
```
108+
There will be a folder called `test-results` created in the directory of the `cypress.config`.
125109

126110
### Add rule to gitignore
127111

@@ -141,7 +125,7 @@ For convenience, you may desire to house the `npx` command within an npm script
141125

142126
```json
143127
"scripts": {
144-
"last-failed": "npx cypress-run-last-failed run --e2e --browser electron"
128+
"last-failed": "npx cypress-plugin-last-failed run --e2e --browser electron"
145129
}
146130
```
147131

@@ -198,7 +182,7 @@ jobs:
198182
if: always()
199183
uses: cypress-io/github-action@v6
200184
with:
201-
command: npx cypress-last-failed run
185+
command: npx cypress-plugin-last-failed run
202186
working-directory: ${{ github.workspace }}
203187
```
204188

Diff for: assets/cypress-last-failed.png

98.5 KB
Loading

Diff for: cypress.config.js

-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ const { collectFailingTests } = require('./src/index');
33

44
module.exports = defineConfig({
55
env: {
6-
failedTestDirectory: './',
76
grepOmitFiltered: true,
87
grepFilterSpecs: true,
98
},

Diff for: package-lock.json

+3-3
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Diff for: package.json

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
{
22
"name": "cypress-plugin-last-failed",
3-
"version": "1.0.0",
3+
"version": "1.0.1",
44
"description": "Cypress plugin to rerun last failed tests in cypress run and open mode",
55
"main": "./src/index.js",
66
"scripts": {
7-
"last-failed": "node runFailed.js run --e2e --browser electron"
7+
"last-failed": "npx cypress-plugin-last-failed run --e2e --browser electron"
88
},
99
"bin": {
10-
"cypress-last-failed": "runFailed.js"
10+
"cypress-plugin-last-failed": "runFailed.js"
1111
},
1212
"keywords": [
1313
"cypress",

Diff for: runFailed.js

+5-10
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,13 @@ const cypress = require('cypress');
44
const fs = require('fs');
55
const path = require('path');
66
const appDir = process.env.INIT_CWD ? process.env.INIT_CWD : path.resolve('.');
7-
const cypressConfig = require(`${appDir}/cypress.config`);
87

98
async function runLastFailed() {
10-
const noFailedTestsMessage =
11-
'No previous failed tests detected\nTry running tests again with cypress run';
12-
13-
// Use the cypress environment variable failedTestDirectory for where to store failed tests
14-
// If not set then use the directory of the project's cypress.config where the test-results defaults to
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`;
1914

2015
if (fs.existsSync(failedTestFilePath)) {
2116
// Retrieve the failedTests from the file

Diff for: src/index.js

+6-10
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,7 @@ const path = require('path');
66
*
77
* After each run, a file will store failed test titles within a test-results directory
88
*
9-
* Environment variable `failedTestDirectory` can be used to set a specific directory to store test-results
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
1210
*
1311
* Subsequent test runs containing failed tests will overwrite this file
1412
* @param {*} on
@@ -17,7 +15,6 @@ const path = require('path');
1715
*/
1816

1917
const collectFailingTests = (on, config) => {
20-
// Check for environment variable `collectFailingTests` to be true
2118
on('after:run', async (results) => {
2219
let failedTests = [];
2320
// Grab every failed test's title
@@ -36,13 +33,12 @@ const collectFailingTests = (on, config) => {
3633
// Prepare a string that can be read from cy-grep
3734
const greppedTestFormat = stringedTests.replaceAll(',', '; ');
3835

39-
// Use the cypress.config environment variable for failedTestDirectory
40-
// If not set then use the cypress.config directory
41-
const failedTestFileDirectory =
42-
config.env.failedTestDirectory === undefined
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/`;
4540

41+
// Create the directory and last-run file where failed tests will be written to
4642
await fs.promises.mkdir(`${failedTestFileDirectory}`, {
4743
recursive: true,
4844
});

0 commit comments

Comments
 (0)