Skip to content

Commit 4b49d70

Browse files
Merge pull request #12 from dennisbergevin/dennis/fix-format
chore: Fix formatting and add new workflow job
2 parents 7f12221 + aa31039 commit 4b49d70

File tree

8 files changed

+63
-37
lines changed

8 files changed

+63
-37
lines changed

Diff for: .github/workflows/main.yml

+23-2
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ on:
77
workflow_dispatch:
88

99
jobs:
10-
node-script:
10+
default-test-results:
1111
runs-on: ubuntu-22.04
1212
steps:
1313
- name: Checkout 📦
@@ -19,10 +19,31 @@ jobs:
1919
if: always()
2020
run: |
2121
cat ./test-results/last-run.json
22-
- name: Custom tests 🧪
22+
- name: Run failed tests from default directory 🧪
2323
if: always()
2424
uses: cypress-io/github-action@v6
2525
with:
2626
# environment variable used for CI/CD tests in this repo
2727
command: npx cypress-plugin-last-failed run --env shouldPass=true
2828
working-directory: ${{ github.workspace }}
29+
custom-test-results:
30+
runs-on: ubuntu-22.04
31+
steps:
32+
- name: Checkout 📦
33+
uses: actions/checkout@v4
34+
- name: Cypress run - store failed tests in custom directory 👟
35+
uses: cypress-io/github-action@v6
36+
with:
37+
command: npm run custom-dir-run
38+
working-directory: ${{ github.workspace }}
39+
continue-on-error: true
40+
- name: Output the file contents 📝
41+
if: always()
42+
run: |
43+
cat ./cypress/config/test-results/last-run.json
44+
- name: Run failed tests from custom directory 🧪
45+
if: always()
46+
uses: cypress-io/github-action@v6
47+
with:
48+
command: npm run custom-dir-last-failed
49+
working-directory: ${{ github.workspace }}

Diff for: README.md

+12-9
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,10 @@ A companion Cypress plugin for <code>cy-grep</code> that re-runs the last failed
1919
#### Table of Contents
2020

2121
- [Features](#features)
22-
- [Table of Contents](#table-of-contents)
22+
- [Table of Contents](#table-of-contents)
2323
- [📦 Installation](#-installation)
2424
- [👟 Run mode](#-run-mode)
25+
- [Specify a custom test-results directory](#specify-a-custom-test-results-directory)
2526
- [Add rule to gitignore](#add-rule-to-gitignore)
2627
- [📃 Setting up a `npm` script](#-setting-up-a-npm-script)
2728
- [⌛ Open mode](#-open-mode)
@@ -45,9 +46,9 @@ npm install --save-dev cypress-plugin-last-failed
4546
2. In `cypress/support/e2e.js` (For E2E tests) and/or `cypress/support/component.js` (For Component tests),
4647

4748
```js
48-
import { failedTestToggle } from "cypress-plugin-last-failed";
49+
import { failedTestToggle } from 'cypress-plugin-last-failed';
4950

50-
const registerCypressGrep = require("@bahmutov/cy-grep");
51+
const registerCypressGrep = require('@bahmutov/cy-grep');
5152
registerCypressGrep();
5253

5354
failedTestToggle();
@@ -56,8 +57,8 @@ failedTestToggle();
5657
3. In `cypress.config`, include the following within `setupNodeEvents` for `e2e` and/or `component` testing:
5758

5859
```js
59-
const { defineConfig } = require("cypress");
60-
const { collectFailingTests } = require("cypress-plugin-last-failed");
60+
const { defineConfig } = require('cypress');
61+
const { collectFailingTests } = require('cypress-plugin-last-failed');
6162

6263
module.exports = defineConfig({
6364
screenshotOnRunFailure: false,
@@ -69,15 +70,15 @@ module.exports = defineConfig({
6970
setupNodeEvents(on, config) {
7071
collectFailingTests(on, config);
7172

72-
require("@bahmutov/cy-grep/src/plugin")(config);
73+
require('@bahmutov/cy-grep/src/plugin')(config);
7374
return config;
7475
},
7576
},
7677
component: {
7778
setupNodeEvents(on, config) {
7879
collectFailingTests(on, config);
7980

80-
require("@bahmutov/cy-grep/src/plugin")(config);
81+
require('@bahmutov/cy-grep/src/plugin')(config);
8182
return config;
8283
},
8384
},
@@ -108,6 +109,8 @@ You can also include more cli arguments similar to `cypress run`, as the command
108109
npx cypress-plugin-last-failed run --e2e --browser chrome
109110
```
110111

112+
### Specify a custom test-results directory
113+
111114
There will be a folder called test-results created in the directory of the cypress.config. If you would like to specify a different folder for test results, use the LAST_FAILED_RESULTS_PATH environment variable:
112115

113116
```bash
@@ -171,7 +174,7 @@ Normally, any Cypress test or suite of tests marked with a `.skip` will be shown
171174
Since this plugin uses `@bahmutov/cy-grep` plugin, we can instead designate skipped tests using a **required tag**:
172175

173176
```js
174-
it("deletes an item", { requiredTags: "@skip" }, () => {
177+
it('deletes an item', { requiredTags: '@skip' }, () => {
175178
expect(1).to.equal(2);
176179
});
177180
```
@@ -195,7 +198,7 @@ name: test-last-failed-node-script
195198
on:
196199
push:
197200
branches:
198-
- "main"
201+
- 'main'
199202
pull_request:
200203
workflow_dispatch:
201204

Diff for: cypress/config/cypress.config.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
const { defineConfig } = require("cypress");
2-
const { collectFailingTests } = require("../../src/index");
1+
const { defineConfig } = require('cypress');
2+
const { collectFailingTests } = require('../../src/index');
33

44
module.exports = defineConfig({
55
env: {
@@ -9,15 +9,15 @@ module.exports = defineConfig({
99
screenshotOnRunFailure: false,
1010
e2e: {
1111
setupNodeEvents(on, config) {
12-
require("@bahmutov/cy-grep/src/plugin")(config);
12+
require('@bahmutov/cy-grep/src/plugin')(config);
1313
collectFailingTests(on, config);
1414

1515
return config;
1616
},
1717
},
1818
component: {
1919
setupNodeEvents(on, config) {
20-
require("@bahmutov/cy-grep/src/plugin")(config);
20+
require('@bahmutov/cy-grep/src/plugin')(config);
2121
collectFailingTests(on, config);
2222

2323
return config;

Diff for: package-lock.json

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

Diff for: package.json

+4-2
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
{
22
"name": "cypress-plugin-last-failed",
3-
"version": "2.0.1",
3+
"version": "2.0.2",
44
"description": "Cypress plugin to rerun last failed tests in cypress run and open mode",
55
"main": "./src/index.js",
66
"types": "./src/index.d.ts",
77
"scripts": {
8-
"last-failed": "npx cypress-plugin-last-failed run --e2e --browser electron"
8+
"last-failed": "npx cypress-plugin-last-failed run --e2e --browser electron",
9+
"custom-dir-run": "LAST_FAILED_RESULTS_PATH=cypress/config npx cypress run",
10+
"custom-dir-last-failed": "LAST_FAILED_RESULTS_PATH=cypress/config npx cypress-plugin-last-failed run --env shouldPass=true"
911
},
1012
"bin": {
1113
"cypress-plugin-last-failed": "runFailed.js"

Diff for: runFailed.js

+10-10
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
#!/usr/bin/env node
22

3-
const cypress = require("cypress");
4-
const fs = require("fs");
5-
const path = require("path");
6-
const appDir = process.env.INIT_CWD ? process.env.INIT_CWD : path.resolve(".");
3+
const cypress = require('cypress');
4+
const fs = require('fs');
5+
const path = require('path');
6+
const appDir = process.env.INIT_CWD ? process.env.INIT_CWD : path.resolve('.');
77

88
async function runLastFailed() {
99
const noFailedTestsMessage = `No previous failed tests detected
@@ -14,14 +14,14 @@ Try running tests again with cypress run`;
1414
? path.join(
1515
appDir,
1616
process.env.LAST_FAILED_RESULTS_PATH,
17-
"test-results",
18-
"last-run.json"
17+
'test-results',
18+
'last-run.json'
1919
)
20-
: path.join(appDir, "test-results", "last-run.json");
20+
: path.join(appDir, 'test-results', 'last-run.json');
2121

2222
if (fs.existsSync(failedTestFilePath)) {
2323
// Retrieve the failedTests from the file
24-
const failedTests = await fs.promises.readFile(failedTestFilePath, "utf8");
24+
const failedTests = await fs.promises.readFile(failedTestFilePath, 'utf8');
2525

2626
// Retrieve the parent suite and tests in the results from test-results/last-run
2727
const parentAndTest = JSON.parse(failedTests).map(({ parent, test }) => ({
@@ -31,13 +31,13 @@ Try running tests again with cypress run`;
3131
// Combine parent suite and test together
3232
const resultSet = new Set(
3333
Object.values(parentAndTest).flatMap(
34-
(parent) => parent.parent + "," + parent.test + ";"
34+
(parent) => parent.parent + ',' + parent.test + ';'
3535
)
3636
);
3737
// Format string for use in grep functionality
3838
const stringedTests = Array.from(resultSet)
3939
.toString()
40-
.replaceAll(",", " ")
40+
.replaceAll(',', ' ')
4141
.slice(0, -1);
4242

4343
if (stringedTests.length > 0) {

Diff for: src/index.d.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// index.d.ts
2-
declare module "cypress-plugin-last-failed" {
2+
declare module 'cypress-plugin-last-failed' {
33
/**
44
* Collects failed tests from the most recent Cypress test run.
55
*

Diff for: src/index.js

+7-7
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
const fs = require("fs");
2-
const path = require("path");
3-
const failedTestToggle = require("./toggle");
1+
const fs = require('fs');
2+
const path = require('path');
3+
const failedTestToggle = require('./toggle');
44

55
/**
66
* Collects failed tests from the most recent Cypress test run
@@ -15,13 +15,13 @@ const failedTestToggle = require("./toggle");
1515
* @returns
1616
*/
1717
const collectFailingTests = (on, config) => {
18-
on("after:run", async (results) => {
18+
on('after:run', async (results) => {
1919
let failedTests = [];
2020

2121
for (const run of results.runs) {
2222
if (run.tests && run.spec) {
2323
const tests = run.tests
24-
.filter((test) => test.state === "failed")
24+
.filter((test) => test.state === 'failed')
2525
.map((test) => test.title);
2626

2727
const spec = run.spec.relative;
@@ -35,13 +35,13 @@ const collectFailingTests = (on, config) => {
3535
? path.join(
3636
process.env.INIT_CWD,
3737
process.env.LAST_FAILED_RESULTS_PATH,
38-
"test-results"
38+
'test-results'
3939
)
4040
: `${path.dirname(config.configFile)}/test-results/`;
4141

4242
// Create the directory and last-run file where failed tests will be written to
4343
await fs.promises.mkdir(failedTestFilePath, { recursive: true });
44-
const lastRunReportFile = path.join(failedTestFilePath, "last-run.json");
44+
const lastRunReportFile = path.join(failedTestFilePath, 'last-run.json');
4545
await fs.promises.writeFile(lastRunReportFile, JSON.stringify(failedTests));
4646
});
4747

0 commit comments

Comments
 (0)