-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
executable file
·190 lines (167 loc) · 4.85 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
#!/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 isValidPassing = (x) => typeof x === 'number' && x > 0
// 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
},
{
// 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 noOptionsSpecified =
!isPassingSpecified &&
!isMinPassingSpecified &&
!isPendingSpecified &&
!isFailingSpecified
debug('specified options %o', {
isPassingSpecified,
isMinPassingSpecified,
isFailingSpecified,
isPendingSpecified,
noOptionsSpecified,
})
if (noOptionsSpecified) {
console.error('Need to specify at least one parameter:')
console.error('--passing or --min-passing or --failing or --pending')
process.exit(1)
}
if (isPassingSpecified) {
if (!isValidPassing(args['--passing'])) {
console.error('expected a number of --passing tests', args['--passing'])
process.exit(1)
}
}
if (isMinPassingSpecified) {
if (!isValidPassing(args['--min-passing'])) {
console.error(
'expected a number of --min-passing tests',
args['--min-passing'],
)
process.exit(1)
}
}
if (isFailingSpecified) {
if (!isValidPassing(args['--failing'])) {
console.error('expected a number of --failing tests', args['--failing'])
process.exit(1)
}
}
if (isPassingSpecified && isMinPassingSpecified) {
console.error('Cannot specify both --passing and --min-passing options')
process.exit(1)
}
debug('params %o', {
passing: args['--passing'],
minPassing: args['--min-passing'],
failing: args['--failing'],
pending: args['--pending'],
})
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)
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') {
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)
})