-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathindex.js
executable file
·238 lines (208 loc) · 7.35 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
#!/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');
const minimatch = require('minimatch');
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-file'],
type: 'string',
description: 'Path to the config file to be used.\nIf false is passed, no config file will be used.',
default: 'cypress.config.js',
global: true,
})
.option('config', {
alias: ['c', 'config'],
type: 'string',
description: 'Set configuration values. Separate multiple values with commas. The values set here override any values set in your configuration file.',
global: true,
})
.option('specPattern', {
alias: ['i', 'integration'],
type: 'string',
description: 'A glob pattern of the test files to load.',
default: 'cypress/e2e',
global: true,
})
.option('excludeSpecPattern', {
type: 'array',
default: [],
description: 'Array with the list of the files to exclude',
global: true,
})
.option('testFiles', {
type: 'string',
description: 'Glob pattern of the test files to load',
global: true,
})
.help()
.alias('help', 'h')
.demandCommand()
.showHelpOnFail(true)
.wrap(Math.min(120, yargs.terminalWidth))
.argv;
async function createTestSample(specIdentifiers) {
try {
const results = await cypress.run({
...argv,
config: argv.config,
configFile: argv.configFile,
spec: castArray(specIdentifiers).join(','),
quiet: true,
});
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();
// Verify if the testFiles argv exists, otherwise use the default value
argv.testFiles = argv.testFiles || '**/*.*';
// Read user-specified spec files from filesystem
let specFiles;
argv.fileIdentifiers = castArray(argv.fileIdentifiers);
try {
specFiles = glob.sync(argv.specPattern + '/' + argv.testFiles)
.filter(fileName => {
return (
argv.fileIdentifiers.some((fileIdentifier) => fileName.toLowerCase().includes(fileIdentifier))
);
});
if (argv.excludeSpecPattern.length > 0) {
const MINIMATCH_OPTIONS = { dot: true, matchBase: true };
specFiles = specFiles.filter(specFile =>
!argv.excludeSpecPattern.some(excludePattern =>
minimatch(specFile, excludePattern, MINIMATCH_OPTIONS)
)
);
}
} catch (err) {
if (err.code === 'ENOENT') {
console.warn(`
Could not load Cypress spec files at path: \`${argv.specPattern}\`
Specify the path to your test files with the \`--specPattern\` 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:
\`${argv.specPattern}\`
See help for the \`--specPattern\` 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]();