-
-
Notifications
You must be signed in to change notification settings - Fork 24
/
index.js
345 lines (301 loc) · 11.2 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
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
/// <reference types="Cypress" />
// @ts-check
const debug = require('debug')('cypress-split')
const ghCore = require('@actions/core')
const { parseSplitInputs, getSpecsToSplit } = require('./parse-inputs')
const { hasTimeDifferences, mergeTimings } = require('./timings')
const { getEnvironmentFlag, splitSpecsLogic } = require('./utils')
const path = require('path')
const os = require('os')
const fs = require('fs')
const humanizeDuration = require('humanize-duration')
const label = 'cypress-split:'
const isDefined = (x) => typeof x !== 'undefined'
const hasSpecPassed = (specResult) =>
specResult.stats.passes > 0 && specResult.stats.failures === 0
const hasFailedTests = (specResult) => specResult.stats.failures > 0
/**
* Initialize Cypress split plugin using Cypress "on" and "config" arguments.
* @param {Cypress.PluginEvents} on Cypress "on" event registration
* @param {Cypress.Config} config Cypress config object
*/
function cypressSplit(on, config) {
// maybe the user called this function with a single argument
// then we assume it is the config object
if (arguments.length === 1) {
debug('single argument, assuming it is the config object')
// @ts-ignore
config = on
}
if (config.spec) {
debug('config has specs set')
debug(config.spec)
}
// if we want to see all settings
// console.log(config)
// the user can specify the split flag / numbers
// using either OS process environment variables
// or Cypress env variables
debug('Cypress config env')
debug(config.env)
debug('current working directory %s', process.cwd())
debug('project root folder %s', config.projectRoot)
// collect the test results to generate a better report
const specResults = {}
// map from absolute to relative spec names as reported by Cypress
const specAbsoluteToRelative = {}
on('after:spec', (spec, results) => {
// console.log(results, results)
const passed = results.stats.failures === 0 && results.stats.passes > 0
if (passed) {
debug('after:spec for passed %s %o', spec.relative, results.stats)
} else {
debug('after:spec for %s %o', spec.relative, results.stats)
}
const cwd = process.cwd()
const absoluteSpecPath = spec.absolute || path.join(cwd, spec.relative)
// make sure there are no duplicate specs for some reason
if (specResults[absoluteSpecPath]) {
console.error(
'Warning: cypress-split found duplicate test results for %s',
absoluteSpecPath,
)
}
specResults[absoluteSpecPath] = results
specAbsoluteToRelative[absoluteSpecPath] = spec.relative
})
let { SPLIT, SPLIT_INDEX, SPLIT_FILE, SPLIT_OUTPUT_FILE, ciName } =
parseSplitInputs(process.env, config.env)
if (SPLIT_FILE) {
console.log('%s Timings are read from %s', label, SPLIT_FILE)
}
if (SPLIT_OUTPUT_FILE) {
console.log('%s Timings will be written to %s', label, SPLIT_OUTPUT_FILE)
}
if (ciName) {
console.log(
'%s detected %s machine %d of %d',
label,
ciName,
SPLIT_INDEX + 1,
SPLIT,
)
}
if (isDefined(SPLIT) && isDefined(SPLIT_INDEX)) {
const specs = getSpecsToSplit(process.env, config)
console.log('%s there are %d found specs', label, specs.length)
// console.log(specs)
const splitN = Number(SPLIT)
const splitIndex = Number(SPLIT_INDEX)
console.log('%s chunk %d of %d', label, splitIndex + 1, splitN)
debug('get chunk %o', { specs, splitN, splitIndex })
const { splitSpecs, foundSplitFile } = splitSpecsLogic({
specs,
splitN,
splitIndex,
splitFileName: SPLIT_FILE,
label,
})
debug('split specs')
debug(splitSpecs)
const addSpecResults = () => {
let chunkPassed = true
// at this point, the specAbsoluteToRelative object should be filled
const specRows = splitSpecs.map((absoluteSpecPath, k) => {
const relativeName = specAbsoluteToRelative[absoluteSpecPath]
const specRow = [String(k + 1), relativeName || absoluteSpecPath]
const specResult = specResults[absoluteSpecPath]
if (specResult) {
// shorted to relative filename
debug('spec results for %s', relativeName)
debug(specResult.stats)
// the duration field depends on the Cypress version
const specDuration = Math.round(
specResult.stats.duration || specResult.stats.wallClockDuration,
)
const humanSpecDuration = humanizeDuration(specDuration)
debug(
'spec took %d ms, human duration %s',
specDuration,
humanSpecDuration,
)
const specPassed = !hasFailedTests(specResult)
chunkPassed = chunkPassed && specPassed
// have to convert numbers to strings
specRow.push(String(specResult.stats.passes))
specRow.push(String(specResult.stats.failures))
specRow.push(String(specResult.stats.pending))
specRow.push(String(specResult.stats.skipped))
specRow.push(humanSpecDuration)
} else {
console.error('Could not find spec results for %s', absoluteSpecPath)
}
return specRow
})
return { specRows, chunkPassed }
}
on('after:run', () => {
if (SPLIT_FILE) {
console.log('%s here are passing spec timings', label)
const specDurations = splitSpecs
.map((absoluteSpecPath, k) => {
const relativeName = specAbsoluteToRelative[absoluteSpecPath]
const specResult = specResults[absoluteSpecPath]
if (specResult) {
const passed = hasSpecPassed(specResult)
const allPending =
specResult.stats.tests === specResult.stats.pending
debug({ relativeName, passed, allPending })
if (passed || allPending) {
const duration = Math.round(
specResult.stats.duration ||
specResult.stats.wallClockDuration,
)
debug('new info %o', { relativeName, duration })
return {
spec: relativeName,
duration,
}
} else {
debug('spec %s has not passed, ignoring timing', relativeName)
return
}
} else {
return
}
})
.filter(Boolean)
const timings = {
durations: specDurations,
}
const timingsString = JSON.stringify(timings, null, 2)
console.log(timingsString)
if (!foundSplitFile) {
console.log(
'%s writing out timings file %s',
label,
SPLIT_OUTPUT_FILE,
)
fs.writeFileSync(SPLIT_OUTPUT_FILE, timingsString + '\n', 'utf8')
} else {
const splitFile = JSON.parse(fs.readFileSync(foundSplitFile, 'utf8'))
let splitThreshold = 0.1
if (
'SPLIT_TIME_THRESHOLD' in process.env &&
process.env.SPLIT_TIME_THRESHOLD
) {
debug(
'will use SPLIT_TIME_THRESHOLD value %s',
process.env.SPLIT_TIME_THRESHOLD,
)
splitThreshold = parseFloat(process.env.SPLIT_TIME_THRESHOLD)
debug('parsed SPLIT_TIME_THRESHOLD is %d', splitThreshold)
}
const hasUpdatedTimings = hasTimeDifferences(
splitFile,
timings,
splitThreshold,
)
if (hasUpdatedTimings) {
// TODO: merge split file with new timings
// do not forget specs not present in the current run!
const mergedTimings = mergeTimings(splitFile, timings)
const mergedText = JSON.stringify(mergedTimings, null, 2)
console.log(
'%s writing out updated timings file %s',
label,
SPLIT_OUTPUT_FILE,
)
debug('previous timings has %d entries', splitFile.durations.length)
debug('current timings has %d entries', timings.durations.length)
debug(
'merged timings has %d entries written to %s',
mergedTimings.durations.length,
SPLIT_OUTPUT_FILE,
)
fs.writeFileSync(SPLIT_OUTPUT_FILE, mergedText + '\n', 'utf8')
} else {
console.log('%s spec timings unchanged', label)
if (SPLIT_OUTPUT_FILE !== SPLIT_FILE) {
console.log(
'%s writing out timings file %s',
label,
SPLIT_OUTPUT_FILE,
)
fs.writeFileSync(SPLIT_OUTPUT_FILE, timingsString + '\n', 'utf8')
}
}
}
}
const shouldWriteSummary = getEnvironmentFlag('SPLIT_SUMMARY', true)
debug('shouldWriteSummary', shouldWriteSummary)
if (shouldWriteSummary) {
// only output the GitHub summary table AFTER the run
// because GH does not show the summary before the job finishes
// so we might as well wait for all spec results to come in
if (process.env.GITHUB_ACTIONS) {
const { specRows, chunkPassed } = addSpecResults()
const chunkEmoji = chunkPassed ? '✅' : '❌'
const chunkHeading = `${label} chunk ${splitIndex + 1} of ${splitN} (${
splitSpecs.length
} ${splitSpecs.length === 1 ? 'spec' : 'specs'}) ${chunkEmoji}`
// https://github.blog/2022-05-09-supercharging-github-actions-with-job-summaries/
ghCore.summary
.addHeading(chunkHeading)
.addTable([
[
{ data: 'K', header: true },
{ data: 'Spec', header: true },
{ data: 'Passed ✅', header: true },
{ data: 'Failed ❌', header: true },
{ data: 'Pending ✋', header: true },
{ data: 'Skipped ↩️', header: true },
{ data: 'Duration 🕗', header: true },
],
...specRows,
])
.addLink(
'bahmutov/cypress-split',
'https://github.com/bahmutov/cypress-split',
)
.write()
}
}
})
if (splitSpecs.length) {
debug('setting the spec pattern to')
debug(splitSpecs)
// if working with Cypress v9, there is integration folder
// @ts-ignore
if (config.integrationFolder) {
debug('setting test files')
// @ts-ignore
config.testFiles = splitSpecs.map((name) =>
// @ts-ignore
path.relative(config.integrationFolder, name),
)
} else {
// Cypress v10+
config.specPattern = splitSpecs
}
} else {
// copy the empty spec file from our source folder into temp folder
const tempFilename = path.join(
os.tmpdir(),
`empty-${splitIndex + 1}-of-${splitN}.cy.js`,
)
const emptyFilename = path.resolve(__dirname, 'empty-spec.cy.js')
fs.copyFileSync(emptyFilename, tempFilename)
console.log(
'%s no specs to run, running an empty spec file %s',
label,
tempFilename,
)
config.specPattern = tempFilename
}
return config
} else {
debug('no SPLIT or SPLIT_INDEX, not splitting specs')
}
}
module.exports = cypressSplit