forked from trentrand/cypress-utils
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
executable file
·266 lines (231 loc) · 8.15 KB
/
index.js
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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
#!/usr/bin/env node
const fs = require('fs');
const { performance } = require('perf_hooks');
const path = require('path');
const yargs = require('yargs');
const mapLimit = require('async/mapLimit');
const capitalize = require('lodash/capitalize');
const castArray = require('lodash/castArray');
const groupBy = require('lodash/groupBy');
const cypress = require('cypress');
const glob = require('glob');
var argv = yargs.scriptName('cypress-utils')
.usage('$0 <cmd> [args]')
.example('$0 stress-test foo.spec.js', 'stress test the "foo" spec file')
.example('$0 stress-test bar', 'stress test the matched `bar.spec.js` file')
.command('run-parallel [fileIdentifiers..]', 'Parallelize your local Cypress run', (yargs) => {
yargs
.positional('fileIdentifiers', {
type: 'string',
describe: 'A unique identifier for the spec file to test.\nIf not specified, all spec files will be ran.',
default: '',
})
})
.command('stress-test [fileIdentifiers..]', 'Stress test a Cypress spec file', (yargs) => {
yargs
.positional('fileIdentifiers', {
type: 'string',
describe: 'A unique identifier for the spec file to test',
default: '',
})
.option('trialCount', {
alias: ['n', 'count'],
type: 'number',
description: 'Number of trial attempts to run test',
default: 4,
})
})
.option('threads', {
alias: ['t', 'limit'],
type: 'number',
description: 'Maximum number of parallel test runners',
default: 2,
global: true,
})
.option('configFile', {
alias: ['c', 'config'],
type: 'string',
description: 'Path to the config file to be used.\nIf false is passed, no config file will be used.',
default: 'cypress.json',
global: true,
})
.option('integrationFolder', {
alias: ['i', 'integration'],
type: 'string',
description: 'Path to folder containing integration test files',
default: 'cypress/integration',
global: true,
})
.option('testFiles', {
type: 'string',
description: 'Glob pattern of the test files to load',
global: true,
})
.option('browser', {
alias: ['b'],
type: 'string',
description: 'Browser in which the test is started.',
default: 'electron',
global: true
})
.option('headless', {
type: 'boolean',
description: 'Use headless mode',
default: false,
global: true
})
.option('quiet', {
type: 'boolean',
description: 'Use quiet mode',
default: true,
global: true
})
.help()
.alias('help', 'h')
.demandCommand()
.strict()
.showHelpOnFail(true)
.wrap(Math.min(120, yargs.terminalWidth))
.argv
async function createTestSample(specIdentifiers) {
try {
const results = await cypress.run({
config: cypressConfig,
configFile: argv.configFile,
spec: castArray(specIdentifiers).join(','),
reporter: 'list',
browser: argv.browser,
headless: argv.headless,
quiet: argv.quiet,
});
return results;
}
catch (error) {
throw error;
}
}
function resultIsSuccess(result) {
return result.failures === undefined;
}
function computeResults(results) {
const processedResults = results.filter(resultIsSuccess).map(({ runs }) => runs).flat();
const resultsBySubject = groupBy(processedResults, (result) => {
return result.spec.name.split('.')[0]
});
return Object.entries(resultsBySubject).reduce((statsBySubject, [subjectName, subjectResults]) => {
statsBySubject[subjectName] = subjectResults.reduce((subjectStats, { stats: sampleStats }) => {
for (let statIdentifier in sampleStats) {
// Filter out the wall clock attributes, these don't sum correctly because tests can run in parallel
if (statIdentifier === 'startedAt' || statIdentifier === 'endedAt' || statIdentifier === 'duration') {
continue;
}
// Also filter out the `suites` property. This property does not provide value to the results
if (statIdentifier === 'suites') {
continue;
}
subjectStats[statIdentifier] = (subjectStats[statIdentifier] || 0) + sampleStats[statIdentifier];
}
return subjectStats;
}, {});
return statsBySubject;
}, {});
}
function handleResults(error, results) {
if (error) {
throw error;
}
// Clear command-line before printing results
process.stdout.write('\033c')
const processEndTime = performance.now();
const elapsedTimeInSeconds = parseInt((processEndTime - processStartTime) / 1000);
console.log(`Command completed in ${elapsedTimeInSeconds} seconds.\n`);
const formattedResults = computeResults(results);
Object.entries(formattedResults).forEach(([subjectName, subjectResults]) => {
printResultsForCommand(subjectName, subjectResults, command);
});
}
function formatTableData(tableData) {
return {
Results: Object.fromEntries(Object.entries(tableData).map(([key, value]) => [capitalize(key), value]))
};
}
function printResultsForCommand(subjectName, subjectResults, command) {
switch (command) {
case 'stress-test':
console.log(`\nResults for ${argv.trialCount} samples of the '${subjectName}' test:\n`);
break;
case 'run-parallel':
console.log(`\nResults for the '${subjectName}' test:\n`);
break;
}
console.table(formatTableData(subjectResults));
}
// Keep track of elapsed time
const processStartTime = performance.now();
// Initialize the Cypress runner configuration
let cypressConfig = {};
try {
// Passing `--configFile false` will explicitly _not_ attempt to load a configuration file
if (argv.configFile !== 'false') {
cypressConfig = JSON.parse(fs.readFileSync(path.resolve(process.cwd(), argv.configFile), 'utf8')) || {};
}
} catch (err) {
if (err.code === 'ENOENT') {
console.warn(`
Could not load a Cypress configuration at path: \`${argv.configFile}\`
Specify the path to your configuration with the \`--configFile\` command-line option,
or run this command from the appropriate working directory.`.replace(/ +/g, '')
);
} else {
console.error(err)
}
}
// Fall-back to Cypress configuration option if CLI option is not specified, otherwise use the default value
argv.testFiles = argv.testFiles ?? cypressConfig.testFiles ?? '**/*.*';
// Fall-back to `integrationFolder` option if it doesn't exist in the config
if (cypressConfig.integrationFolder === undefined) {
cypressConfig.integrationFolder = argv.integrationFolder;
}
// Read user-specified spec files from filesystem
let specFiles;
argv.fileIdentifiers = castArray(argv.fileIdentifiers);
try {
specFiles = glob.sync(cypressConfig.integrationFolder + '/' + argv.testFiles)
.filter(fileName => {
return (
argv.fileIdentifiers.some((fileIdentifier) => fileName.toLowerCase().includes(fileIdentifier))
);
})
} catch (err) {
if (err.code === 'ENOENT') {
console.warn(`
Could not load Cypress spec files at path: \`${cypressConfig.integrationFolder}\`
Specify the path to your test files with the \`--integrationFolder\` command-line option,
or ensure your Cypress configuration file is specified and setup correctly.
See the \`--configFile\` command-line option.`.replace(/ +/g, '')
);
} else {
console.error(err)
}
return;
}
if (specFiles.length === 0) {
console.warn(`
No Cypress spec files were found. You may have specified invalid file identifiers.
The following file identifiers were provided: ${argv.fileIdentifiers.map((id => `"${id}"`)).join(', ')}
Your configuration specifies that test files are contained within the following directory:
\`${cypressConfig.integrationFolder}\`
See help for the \`--integrationFolder\` command-line option if this is incorrect.`.replace(/ +/g, '')
);
return;
}
const command = argv._[0];
const commandHandlers = {
'run-parallel': () => mapLimit(specFiles, argv.limit, createTestSample, handleResults),
'stress-test': () => {
const repeatedSpecFiles = Array(argv.trialCount).fill(specFiles).flat();
return mapLimit(repeatedSpecFiles, argv.limit, createTestSample, handleResults);
},
};
// Run the appropriate command by calling its handler
commandHandlers[command]();