/
index.js
executable file
·387 lines (342 loc) · 10.6 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
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
#!/usr/bin/env node
// @ts-check
// runs Cypress with "normal" CLI parameters
// but after the run verifies the expected number of passing tests
const cypress = require('cypress')
const debug = require('debug')('cypress-expect')
const arg = require('arg')
const fs = require('fs')
const R = require('ramda')
const allPaths = require('@bahmutov/all-paths')
const REPO_URL = 'https://github.com/bahmutov/cypress-expect'
const CLI_HELP_URL = 'https://github.com/bahmutov/cypress-expect#options'
const cliHelpMessage = `see ${CLI_HELP_URL}`
const isValidPassing = (x) => typeof x === 'number' && x > 0
/**
* Normalizes different forms of test status to
* @see https://on.cypress.io/writing-and-organizing-tests#Test-statuses
*/
const normalizeTestState = (s) => {
if (s === 'passing' || s === 'pass' || s === 'passes' || s === 'passed') {
return 'passed'
}
if (s === 'failing' || s === 'fail' || s === 'fails' || s === 'failed') {
return 'failed'
}
if (s === 'pend' || s === 'pending') {
return 'pending'
}
if (s === 'skip' || s === 'skipping' || s === 'skipped') {
return 'skipped'
}
console.error('unknown test state name "%s"', s)
return s
}
// remove all our arguments to let Cypress only deal with its arguments
const args = arg(
{
'--passing': Number, // number of total passing tests to expect
'--min-passing': Number, // at least this number of passing tests
'--failing': Number, // number of failing tests to expect
'--pending': Number, // number of pending tests to expect
'--expect': String, // filename of JSON file with test names and statuses
},
{
// allow other flags to be present - to be sent to Cypress CLI
permissive: true,
},
)
debug('args %o', args)
const isPassingSpecified = '--passing' in args
const isMinPassingSpecified = '--min-passing' in args
const isFailingSpecified = '--failing' in args
const isPendingSpecified = '--pending' in args
const isExpectSpecified = '--expect' in args
const noOptionsSpecified =
!isPassingSpecified &&
!isMinPassingSpecified &&
!isPendingSpecified &&
!isFailingSpecified &&
!isExpectSpecified
debug('specified options %o', {
isPassingSpecified,
isMinPassingSpecified,
isFailingSpecified,
isPendingSpecified,
isExpectSpecified,
noOptionsSpecified,
})
if (noOptionsSpecified) {
console.error('Need to specify at least one parameter:')
console.error(
'--passing or --min-passing or --failing or --pending or --expect',
)
console.error(cliHelpMessage)
process.exit(1)
}
const isNumberOptionSpecified =
isPassingSpecified ||
isMinPassingSpecified ||
isFailingSpecified ||
isPendingSpecified
if (isExpectSpecified && isNumberOptionSpecified) {
console.error('You used --expect with some other option')
console.error('--expect <filename> can be the only option by itself')
console.error(cliHelpMessage)
process.exit(1)
}
if (isPassingSpecified) {
if (!isValidPassing(args['--passing'])) {
console.error('expected a number of --passing tests', args['--passing'])
console.error(cliHelpMessage)
process.exit(1)
}
}
if (isMinPassingSpecified) {
if (!isValidPassing(args['--min-passing'])) {
console.error(
'expected a number of --min-passing tests',
args['--min-passing'],
)
console.error(cliHelpMessage)
process.exit(1)
}
}
if (isFailingSpecified) {
if (!isValidPassing(args['--failing'])) {
console.error('expected a number of --failing tests', args['--failing'])
console.error(cliHelpMessage)
process.exit(1)
}
}
if (isPassingSpecified && isMinPassingSpecified) {
console.error('Cannot specify both --passing and --min-passing options')
console.error(cliHelpMessage)
process.exit(1)
}
if (isExpectSpecified) {
const filename = args['--expect']
if (!fs.existsSync(filename)) {
console.error('Cannot find file specified using --expect option')
console.error('filename: "%s"', filename)
console.error(cliHelpMessage)
process.exit(1)
}
}
debug('params %o', {
passing: args['--passing'],
minPassing: args['--min-passing'],
failing: args['--failing'],
pending: args['--pending'],
expect: args['--expect'],
})
const getExpectedTestStatuses = (filename) => {
const text = fs.readFileSync(filename, 'utf-8')
const json = JSON.parse(text)
return json
}
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)
}
// NOTE: modifies the object in place
const removeEmptyLeaves = (json) => {
if (typeof json === 'string') {
return
}
Object.keys(json).forEach((key) => {
removeEmptyLeaves(json[key])
if (R.isEmpty(json[key])) {
delete json[key]
}
})
return json
}
const collectPaths = (json) => {
const list = allPaths(json)
// leave only paths that lead to the test status
// which is a string, effectively filtering out
// all outer suite names
.filter((path) => {
return typeof R.path(path, json) === 'string'
})
return list
}
const testTitle = (parts) => parts.join(' / ')
parseArguments()
.then((options) => {
debug('parsed CLI options %o', options)
return cypress.run(options)
})
.then((runResults) => {
debug('run status %s', runResults.status)
if (!runResults.status) {
// @ts-ignore
if (runResults.failures) {
runResults.status = 'failed'
} else {
runResults.status = 'finished'
}
debug('set run status %s', runResults.status)
}
// see https://on.cypress.io/module-api
if (runResults.status === 'failed' && runResults.failures) {
console.error(runResults.message)
process.exit(1)
}
if (runResults.status !== 'finished') {
console.error(
'cypress-error: Hmm, unknown run status "%s"',
runResults.status,
)
console.error('cypress-error: not sure how to proceed')
console.error('cypress-error: seek help at %s', REPO_URL)
console.error('cypress-error: exiting with an error')
process.exit(1)
}
if (isExpectSpecified) {
// a single object with expected test results
let expectedTestStatuses = getExpectedTestStatuses(args['--expect'])
debug('expected test statuses %o', expectedTestStatuses)
debug('test runs %o', runResults.runs)
// collect every test result reported by Cypress
const tests = []
runResults.runs.forEach((runResult) => {
runResult.tests.forEach((testResult) => {
tests.push({
// title is an array with strings
// from the outer suite title, all the way to the test title
title: testResult.title,
state: testResult.state,
})
})
})
debug('test results %o', tests)
// match every test result with expected test result
// if not found, the only acceptable test state is passing
let didNotMatch = 0
tests.forEach((test) => {
const expectedTestStatus = R.path(test.title, expectedTestStatuses)
if (!expectedTestStatus) {
debug('missing expected state for test "%o"', test.title)
// cannot find the expected test status, should be passing
if (test.state !== 'passed') {
didNotMatch += 1
console.log(
'cypress-expect: expected implicitly the test "%s" to pass, got %s',
test.title.join(' / '),
test.state,
)
}
} else {
// found the expected test record in the JSON file
// let's remove it - by the end of the matching the "expected"
// object will only have expected test results that were NOT
// present in the test results
expectedTestStatuses = R.dissocPath(test.title, expectedTestStatuses)
const normalized = normalizeTestState(expectedTestStatus)
debug(
'test "%s" should status "%s"',
test.title.join(' / '),
normalized,
)
if (test.state !== normalized) {
didNotMatch += 1
console.log(
'cypress-expect: expected the test "%s" to be %s, got %s',
test.title.join(' / '),
normalized,
test.state,
)
}
}
})
if (didNotMatch) {
console.error(
'cypress-expect: %d %s did not match the expected state from %s',
didNotMatch,
didNotMatch === 1 ? 'test' : 'tests',
args['--expect'],
)
console.error('')
process.exit(1)
}
const removedEmpty = removeEmptyLeaves(expectedTestStatuses)
if (!R.isEmpty(removedEmpty)) {
console.error('cypress-expect: expected the find the following tests')
const titles = collectPaths(removedEmpty)
.map(testTitle)
.map((s) => '* ' + s)
console.error(titles.join('\n'))
console.error()
process.exit(1)
}
// nothing else to do
return
}
const totals = {
failed: runResults.totalFailed,
passed: runResults.totalPassed,
pending: runResults.totalPending,
}
debug('test totals %o', totals)
if (isFailingSpecified) {
if (totals.failed !== args['--failing']) {
console.error(
'ERROR: expected %d failing tests, got %d',
args['--failing'],
totals.failed,
)
process.exit(1)
}
} else {
// any unexpected failed tests are bad
if (totals.failed) {
console.error('%d test(s) failed', totals.failed)
process.exit(totals.failed)
}
}
if (isPassingSpecified) {
// make sure the expected number of tests executed
if (totals.passed !== args['--passing']) {
console.error(
'ERROR: expected %d passing tests, got %d',
args['--passing'],
totals.passed,
)
process.exit(1)
}
}
if (isMinPassingSpecified) {
if (totals.passed < args['--min-passing']) {
console.error(
'ERROR: expected at least %d passing tests, got %d',
args['--min-passing'],
totals.passed,
)
process.exit(1)
}
}
if (isPendingSpecified) {
if (totals.pending !== args['--pending']) {
console.error(
'ERROR: expected %d pending tests, got %d',
args['--pending'],
totals.pending,
)
process.exit(1)
}
}
})
.catch((e) => {
console.log('error: %s', e.message)
console.error(e)
process.exit(1)
})