forked from cypress-io/cypress-skip-test
-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
234 lines (201 loc) · 5.57 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
/// <reference path="./index.d.ts" />
// @ts-check
const { _ } = Cypress
const checkBrowserName = (name) => {
if ('isBrowser' in Cypress) {
// use the new v4.0 method
// @ts-ignore
return Cypress.isBrowser(name)
} else {
return Cypress.browser.name === name
}
}
/**
* Cleans up the passed name which could be browser, platform or other.
* @param {string} name The environment or platform or something else, like url
* @returns {string} Normalized name
* @example
* normalizeName('mac') // 'darwin'
* normalizeName('windows') // 'win32'
* normalizeName('WIN') // 'win32'
* normalizeName('localhost') // 'localhost'
*/
const normalizeName = (name) => {
name = name.toLowerCase()
// values are normalized strings we will use
const aliases = {
mac: 'darwin',
windows: 'win32',
win: 'win32'
}
const normalizedName = aliases[name] ? aliases[name] : name
return normalizedName
}
/**
* Returns true if the test is running on the given browser or platform
* or against given url.
* @param {string} name Browser name, platform or url.
* @returns {boolean} Returns true if the test runs against the given condition.
*/
const isOn = (name) => {
if (!_.isString(name)) {
throw new Error('Invalid syntax: isOn expects a string argument')
}
const normalizedName = normalizeName(name)
if (isPlatform(normalizedName)) {
return Cypress.platform === normalizedName
}
if (isBrowser(normalizedName)) {
return checkBrowserName(normalizedName)
}
if (isHeadedName(normalizedName)) {
return headedMatches(normalizedName)
}
if (isEnvironment(name)) {
return true
}
return matchesUrlPart(normalizedName)
}
// @ts-ignore "cy.state" is not in the "cy" type
const getMochaContext = () => cy.state('runnable').ctx
const skip = () => {
const ctx = getMochaContext()
return ctx.skip()
}
const isPlatform = (name) => ['win32', 'darwin', 'linux'].includes(name)
const isBrowser = (name) => ['electron', 'chrome', 'firefox'].includes(name)
const isHeadedName = (name) => ['headed', 'headless'].includes(name)
const isEnvironmentSet = () =>
typeof Cypress.env('ENVIRONMENT') === 'string' && Cypress.env('ENVIRONMENT')
const isAsyncFn = (name) =>
Object.prototype.toString.call(name) === '[object AsyncFunction]'
const headedMatches = (name) => {
if (name === 'headed') {
return Cypress.browser.isHeaded
}
if (name === 'headless') {
return Cypress.browser.isHeadless
}
throw new Error(`Do not know how to treat headed flag "${name}"`)
}
/**
* You can pass custom environment name when running Cypress
* @example
* CYPRESS_ENVIRONMENT=staging npx cypress run
* @param {string} name Is checked against `ENVIRONMENT` value
* @returns {boolean} true if the given argument matches environment string
*/
const isEnvironment = (name) =>
Cypress.env('ENVIRONMENT') && Cypress.env('ENVIRONMENT') === name
const matchesUrlPart = (normalizedName) => {
// assuming name is part of the url, and the baseUrl should be set
const url = Cypress.config('baseUrl') || location.origin
return url && url.includes(normalizedName)
}
const skipOnBool = (flag, cb) => {
if (!_.isBoolean(flag)) {
throw new Error(
'Invalid syntax: cy.skipOn(<boolean flag>), for example cy.skipOn(true)'
)
}
if (cb) {
if (!flag) {
return cb()
}
} else {
cy.log(`skipOn **${flag}**`)
if (flag) {
skip()
}
}
}
/**
* Skips the current test only in the specified browser, platform or against url.
* @param {string|boolean|Function} name - condition, could be platform, browser name, url or true|false.
* @param {() => void} cb - Optional, run the given callback if the condition passes
*/
const skipOn = (name, cb) => {
if (_.isBoolean(name)) {
return skipOnBool(name, cb)
}
if (isAsyncFn(name)) {
return name().then((result) => onlyOnBool(result, cb))
}
if (_.isFunction(name)) {
return onlyOnBool(name(), cb)
}
if (!_.isString(name) || '') {
throw new Error(
'Invalid syntax: cy.skipOn(<name>), for example cy.skipOn("linux")'
)
}
if (cb) {
if (!isOn(name)) {
return cb()
} else {
return it(`Skipping test(s), on ${name}`)
}
} else {
const normalizedName = normalizeName(name)
cy.log(`skipOn **${normalizedName}**`)
if (isOn(name)) {
return skip()
}
}
}
const onlyOnBool = (flag, cb) => {
if (!_.isBoolean(flag)) {
throw new Error(
'Invalid syntax: cy.onlyOn(<boolean>), for example cy.onlyOn(true)'
)
}
if (cb) {
if (flag) {
return cb()
}
} else {
cy.log(`onlyOn **${flag}**`)
if (!flag) {
skip()
}
}
}
/**
* Runs the current test only in the specified browser, platform or against url.
* @param {string|boolean|Function} name - condition, could be platform, browser name, url or true|false.
* @param {() => void} cb - Optional, run the given callback if the condition passes
*/
const onlyOn = (name, cb) => {
if (_.isBoolean(name)) {
return onlyOnBool(name, cb)
}
if (!_.isString(name) || '') {
throw new Error(
'Invalid syntax: cy.onlyOn(<name>), for example cy.onlyOn("linux")'
)
}
if (isAsyncFn(name)) {
return name().then((result) => onlyOnBool(result, cb))
}
if (_.isFunction(name)) {
return onlyOnBool(name(), cb)
}
if (cb) {
if (isOn(name)) {
return cb()
} else {
return it(`Skipping test(s), not on ${name}`)
}
} else {
const normalizedName = normalizeName(name)
cy.log(`onlyOn **${normalizedName}**`)
if (!isOn(name)) {
return skip()
}
}
}
module.exports = {
skipOn,
onlyOn,
isOn
}