-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
executable file
·204 lines (179 loc) · 6.54 KB
/
index.js
File metadata and controls
executable file
·204 lines (179 loc) · 6.54 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
#!/usr/bin/env node
// @ts-check
const debug = require('debug')('cypress-repeat');
const cypress = require('cypress');
const arg = require('arg');
const Bluebird = require('bluebird');
const fs = require('fs');
const path = require('path');
require('dotenv').config();
debug('process argv %o', process.argv);
// Path to the summary file
const summaryFilePath = path.join(process.cwd(), 'cy-repeat-summary.txt');
if (fs.existsSync(summaryFilePath)) {
console.log('Deleting existing summary file');
try {
fs.unlinkSync(summaryFilePath);
console.log('Existing summary file deleted successfully');
} catch (err) {
console.error('Error deleting summary file:', err.message);
}
} else {
console.log('No existing summary file to delete');
}
const args = arg(
{
'-n': Number,
'--until-passes': Boolean,
'--rerun-failed-only': Boolean,
'--force': Boolean,
},
{ permissive: true }
);
const name = 'cypress-repeat-pro:';
const repeatNtimes = args['-n'] || 1;
const untilPasses = args['--until-passes'] || false;
const rerunFailedOnly = args['--rerun-failed-only'] || false;
const forceContinue = args['--force'] || false;
console.log('%s will repeat Cypress command %d time(s)', name, repeatNtimes);
if (untilPasses) console.log('%s but only until it passes', name);
if (rerunFailedOnly) console.log('%s it only reruns specs which have failed', name);
if (forceContinue) console.log('%s will force continue through all iterations', name);
let anyTestFailed = false;
let totalTests = 0;
let totalPassed = 0;
let totalFailed = 0;
let totalSkipped = 0;
let lastRunFailed = false;
/**
* Quick and dirty deep clone
*/
const clone = (x) => JSON.parse(JSON.stringify(x));
const parseArguments = async () => {
const cliArgs = args._;
if (cliArgs[0] !== 'cypress') cliArgs.unshift('cypress');
if (cliArgs[1] !== 'run') cliArgs.splice(1, 0, 'run');
debug('parsing Cypress CLI %o', cliArgs);
return await cypress.cli.parseRunArguments(cliArgs);
};
parseArguments()
.then((options) => {
debug('parsed CLI options %o', options);
const allRunOptions = [];
for (let k = 0; k < repeatNtimes; k++) {
const runOptions = clone(options);
const envVariables = `cypress_repeat_n=${repeatNtimes},cypress_repeat_k=${k + 1}`;
runOptions.env = runOptions.env ? runOptions.env + ',' + envVariables : envVariables;
if (options.record && options.group) {
runOptions.group = options.group;
if (runOptions.group && repeatNtimes > 1) {
runOptions.group += `-${k + 1}-of-${repeatNtimes}`;
}
}
// Add --force option if explicitly requested
if (forceContinue) {
runOptions.force = true;
}
allRunOptions.push(runOptions);
}
return allRunOptions;
})
.then((allRunOptions) => {
return Bluebird.mapSeries(allRunOptions, (runOptions, k, n) => {
const isLastRun = k === n - 1;
console.log('***** %s %d of %d *****', name, k + 1, n);
const onTestResults = (testResults) => {
// Update totals
totalTests += testResults.totalTests || 0;
totalPassed += testResults.totalPassed || 0;
totalFailed += testResults.totalFailed || 0;
totalSkipped += testResults.totalSkipped || 0;
if (testResults.totalFailed) {
console.error('%s run failed', name);
anyTestFailed = true;
if (isLastRun) {
lastRunFailed = true;
}
}
debug('is %d the last run? %o', k, isLastRun);
if (rerunFailedOnly) {
const failedSpecs = testResults.runs
.filter((run) => run.stats.failures !== 0)
.map((run) => run.spec.relative)
.join(',');
if (failedSpecs.length) {
console.log('Failed specs: %s', failedSpecs);
if (!isLastRun) {
allRunOptions[k + 1].spec = failedSpecs;
}
} else {
console.log('All specs passed on run %d of %d', k + 1, n);
if (!forceContinue) {
console.log('Exiting early as no failed specs detected.');
return Promise.reject(new Error('No failures detected, exiting early.'));
}
}
}
if (testResults.status === 'failed') {
if (testResults.failures) {
console.error(testResults.message);
anyTestFailed = true;
if (!forceContinue) {
return Promise.reject(new Error('Test results indicate failures'));
}
}
}
if (untilPasses) {
if (!testResults.totalFailed) {
console.log('%s successfully passed on run %d of %d', name, k + 1, n);
return Promise.reject(new Error('No failures detected, exiting with success.'));
}
console.error('%s run %d of %d failed', name, k + 1, n);
if (!forceContinue && k === n - 1) {
return Promise.reject(new Error('No more attempts left'));
}
} else {
if (testResults.totalFailed) {
console.error('%s run %d of %d failed', name, k + 1, n);
if (!forceContinue && (!rerunFailedOnly || isLastRun)) {
return Promise.reject(new Error('Failures detected and conditions met'));
}
}
}
};
return cypress.run(runOptions).then(onTestResults);
});
})
.finally(() => {
console.log('Entering final result summary block...');
const resultSummary = [
'***** Repeat Run Summary *****',
`Total Tests with repeat: ${totalTests}`,
`Total Passed: ${totalPassed}`,
`Total Failed: ${totalFailed}`,
`Total Skipped: ${totalSkipped}`,
`*****************************`
].join('\n');
console.log(resultSummary);
console.log('Writing result summary to file...');
try {
const absoluteSummaryFilePath = path.resolve(summaryFilePath);
fs.writeFileSync(absoluteSummaryFilePath, resultSummary);
console.log(`Result summary written successfully at: ${absoluteSummaryFilePath}`);
} catch (err) {
console.error('Error writing result summary to file:', err.message);
}
if (rerunFailedOnly && lastRunFailed) {
console.error('***** Some tests failed during the run(s) event after all retry *****');
console.log('Exiting with failure due to test failures.');
process.exit(1);
} else {
console.log('***** finished %d run(s) successfully *****', repeatNtimes);
}
})
.catch((e) => {
console.error('Error:', e.message);
if (!forceContinue) {
console.log('Exiting with failure due to an error.');
}
});