cypress-rerun-failed-specs-sid-g 1.0.13 → 1.0.14

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 (2) hide show
  1. package/package.json +1 -1
  2. package/src/index.js +53 -22
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "cypress-rerun-failed-specs-sid-g",
3
- "version": "1.0.13",
3
+ "version": "1.0.14",
4
4
  "description": "Rerun failed Cypress spec files from previous run",
5
5
  "main": "src/index.js",
6
6
  "types": "types/index.d.ts",
package/src/index.js CHANGED
@@ -10,11 +10,34 @@ function safeDelete(filePath) {
10
10
  }
11
11
  }
12
12
 
13
- function cleanDirectory(dirPath) {
14
- if (!fs.existsSync(dirPath)) return;
15
- fs.readdirSync(dirPath).forEach(file => {
16
- fs.unlinkSync(path.join(dirPath, file));
17
- });
13
+ /**
14
+ * Delete only report files that belong to failed specs
15
+ */
16
+ function deleteReportsForFailedSpecs(reportDir, failedSpecs) {
17
+ if (!fs.existsSync(reportDir)) return;
18
+
19
+ const reportFiles = fs.readdirSync(reportDir).filter(f =>
20
+ f.endsWith(".json")
21
+ );
22
+
23
+ for (const file of reportFiles) {
24
+ const reportPath = path.join(reportDir, file);
25
+
26
+ try {
27
+ const report = JSON.parse(fs.readFileSync(reportPath, "utf8"));
28
+ if (!report.results) continue;
29
+
30
+ for (const result of report.results) {
31
+ if (failedSpecs.includes(result.file)) {
32
+ fs.unlinkSync(reportPath);
33
+ console.log(` Deleted stale report: ${file}`);
34
+ break;
35
+ }
36
+ }
37
+ } catch {
38
+ // ignore invalid JSON
39
+ }
40
+ }
18
41
  }
19
42
 
20
43
  /* ----------------------------------
@@ -22,13 +45,16 @@ function cleanDirectory(dirPath) {
22
45
  ----------------------------------- */
23
46
  function getFailedSpecs(reportDir) {
24
47
  if (!fs.existsSync(reportDir)) {
25
- console.warn("Report directory does not exist");
48
+ console.warn(" Report directory does not exist");
26
49
  return [];
27
50
  }
28
51
 
29
- const files = fs.readdirSync(reportDir).filter(f => f.endsWith(".json"));
52
+ const files = fs.readdirSync(reportDir).filter(f =>
53
+ f.endsWith(".json")
54
+ );
55
+
30
56
  if (!files.length) {
31
- console.warn("No JSON reports found");
57
+ console.warn(" No JSON reports found");
32
58
  return [];
33
59
  }
34
60
 
@@ -36,6 +62,7 @@ function getFailedSpecs(reportDir) {
36
62
 
37
63
  for (const file of files) {
38
64
  let report;
65
+
39
66
  try {
40
67
  report = JSON.parse(
41
68
  fs.readFileSync(path.join(reportDir, file), "utf8")
@@ -49,12 +76,14 @@ function getFailedSpecs(reportDir) {
49
76
  for (const result of report.results) {
50
77
  let hasFailure = false;
51
78
 
52
- (function walk(suites = []) {
79
+ (function walkSuites(suites = []) {
53
80
  for (const suite of suites) {
54
81
  suite.tests?.forEach(test => {
55
- if (test.state === "failed") hasFailure = true;
82
+ if (test.state === "failed") {
83
+ hasFailure = true;
84
+ }
56
85
  });
57
- walk(suite.suites);
86
+ walkSuites(suite.suites);
58
87
  }
59
88
  })(result.suites);
60
89
 
@@ -68,7 +97,7 @@ function getFailedSpecs(reportDir) {
68
97
  }
69
98
 
70
99
  /* ----------------------------------
71
- Cypress plugin
100
+ Cypress plugin entry
72
101
  ----------------------------------- */
73
102
  function cypressRerunFailed(on, config) {
74
103
  const resultsDir = path.join(config.projectRoot, "cypress", "results");
@@ -80,28 +109,30 @@ function cypressRerunFailed(on, config) {
80
109
 
81
110
  console.log("cypress-rerun-failed initialized");
82
111
 
83
- // Fresh run cleanup
84
- on("before:run", () => {
85
- console.log("🧹 Fresh run: cleaning old reports & failed specs");
86
- cleanDirectory(resultsDir);
87
- safeDelete(failedSpecsPath);
88
- });
89
-
90
- // Detect failures after run
112
+ /**
113
+ * AFTER RUN:
114
+ * - detect failed specs
115
+ * - delete ONLY stale reports for failed specs
116
+ * - save failed-specs.json
117
+ */
91
118
  on("after:run", () => {
92
119
  const failedSpecs = getFailedSpecs(resultsDir);
93
120
 
94
121
  if (!failedSpecs.length) {
95
- console.log("No failed specs detected");
122
+ console.log(" No failed specs detected");
123
+ safeDelete(failedSpecsPath); // clean old failures
96
124
  return;
97
125
  }
98
126
 
127
+ // delete only reports for failed specs
128
+ deleteReportsForFailedSpecs(resultsDir, failedSpecs);
129
+
99
130
  fs.writeFileSync(
100
131
  failedSpecsPath,
101
132
  JSON.stringify(failedSpecs, null, 2)
102
133
  );
103
134
 
104
- console.log("Failed specs saved:", failedSpecsPath);
135
+ console.log(" Failed specs saved:", failedSpecsPath);
105
136
  });
106
137
  }
107
138