/
browser.ts
243 lines (193 loc) · 7.4 KB
/
browser.ts
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
import _ from 'lodash'
import Debug from 'debug'
import {
defaultSpecPattern,
options,
breakingOptions,
breakingRootOptions,
testingTypeBreakingOptions,
} from './options'
import type { TestingType } from '@packages/types'
import type { BreakingOption, BreakingOptionErrorKey, OverrideLevel } from './options'
import type { ErrResult } from './validation'
// this export has to be done in 2 lines because of a bug in babel typescript
import * as validation from './validation'
export {
defaultSpecPattern,
options,
breakingOptions,
BreakingOption,
BreakingOptionErrorKey,
ErrResult,
validation,
}
const debug = Debug('cypress:config:browser')
const dashesOrUnderscoresRe = /^(_-)+/
// takes an array and creates an index object of [keyKey]: [valueKey]
function createIndex<T extends Record<string, any>> (arr: Array<T>, keyKey: keyof T, valueKey: keyof T, defaultValueFallback?: any) {
return _.reduce(arr, (memo: Record<string, any>, item) => {
if (item[valueKey] !== undefined) {
memo[item[keyKey]] = item[valueKey]
} else {
memo[item[keyKey]] = defaultValueFallback
}
return memo
}, {})
}
const breakingKeys = _.map(breakingOptions, 'name')
const defaultValues = createIndex(options, 'name', 'defaultValue')
const publicConfigKeys = _(options).reject({ isInternal: true }).map('name').value()
const validationRules = createIndex(options, 'name', 'validation')
export const testOverrideLevels = createIndex(options, 'name', 'overrideLevel', 'never')
const restartOnChangeOptionsKeys = _.filter(options, 'requireRestartOnChange')
const issuedWarnings = new Set()
export type InvalidTestOverrideResult = {
invalidConfigKey: string
supportedOverrideLevel: string
}
export type BreakingErrResult = {
name: string
newName?: string
value?: any
configFile: string
testingType?: TestingType
}
type ErrorHandler = (
key: BreakingOptionErrorKey,
options: BreakingErrResult
) => void
export function resetIssuedWarnings () {
issuedWarnings.clear()
}
const validateNoBreakingOptions = (breakingCfgOptions: Readonly<BreakingOption[]>, cfg: any, onWarning: ErrorHandler, onErr: ErrorHandler, testingType?: TestingType) => {
breakingCfgOptions.forEach(({ name, errorKey, newName, isWarning, value }) => {
if (_.has(cfg, name)) {
if (value && cfg[name] !== value) {
// Bail if a value is specified but the config does not have that value.
return
}
if (isWarning) {
if (issuedWarnings.has(errorKey)) {
return
}
// avoid re-issuing the same warning more than once
issuedWarnings.add(errorKey)
return onWarning(errorKey, {
name,
newName,
value,
configFile: cfg.configFile,
testingType,
})
}
return onErr(errorKey, {
name,
newName,
value,
configFile: cfg.configFile,
testingType,
})
}
})
}
export const allowed = (obj = {}) => {
const propertyNames = publicConfigKeys.concat(breakingKeys)
return _.pick(obj, propertyNames)
}
export const getBreakingKeys = () => {
return breakingKeys
}
export const getBreakingRootKeys = () => {
return breakingRootOptions
}
export const getDefaultValues = (runtimeOptions: { [k: string]: any } = {}) => {
// Default values can be functions, in which case they are evaluated
// at runtime - for example, slowTestThreshold where the default value
// varies between e2e and component testing.
const defaultsForRuntime = _.mapValues(defaultValues, (value) => (typeof value === 'function' ? value(runtimeOptions) : value))
// As we normalize the config based on the selected testing type, we need
// to do the same with the default values to resolve those correctly
return { ...defaultsForRuntime, ...defaultsForRuntime[runtimeOptions.testingType] }
}
export const getPublicConfigKeys = () => {
return publicConfigKeys
}
export const matchesConfigKey = (key: string) => {
if (_.has(defaultValues, key)) {
return key
}
key = key.toLowerCase().replace(dashesOrUnderscoresRe, '')
key = _.camelCase(key)
if (_.has(defaultValues, key)) {
return key
}
return
}
export const validate = (cfg: any, onErr: (property: ErrResult | string) => void, testingType: TestingType | null) => {
debug('validating configuration', cfg)
return _.each(cfg, (value, key) => {
const validationFn = validationRules[key]
// key has a validation rule & value different from the default
if (validationFn && value !== defaultValues[key]) {
const result = validationFn(key, value, {
testingType,
// TODO: remove with experimentalSessionAndOrigin. Fixed with: https://github.com/cypress-io/cypress/issues/21471
experimentalSessionAndOrigin: cfg.e2e?.experimentalSessionAndOrigin || cfg.experimentalSessionAndOrigin,
})
if (result !== true) {
return onErr(result)
}
}
})
}
export const validateNoBreakingConfigRoot = (cfg: any, onWarning: ErrorHandler, onErr: ErrorHandler, testingType: TestingType) => {
return validateNoBreakingOptions(breakingRootOptions, cfg, onWarning, onErr, testingType)
}
export const validateNoBreakingConfig = (cfg: any, onWarning: ErrorHandler, onErr: ErrorHandler, testingType: TestingType) => {
return validateNoBreakingOptions(breakingOptions, cfg, onWarning, onErr, testingType)
}
export const validateNoBreakingConfigLaunchpad = (cfg: any, onWarning: ErrorHandler, onErr: ErrorHandler) => {
return validateNoBreakingOptions(breakingOptions.filter((option) => option.showInLaunchpad), cfg, onWarning, onErr)
}
export const validateNoBreakingTestingTypeConfig = (cfg: any, testingType: keyof typeof testingTypeBreakingOptions, onWarning: ErrorHandler, onErr: ErrorHandler) => {
const options = testingTypeBreakingOptions[testingType]
return validateNoBreakingOptions(options, cfg, onWarning, onErr, testingType)
}
export const validateOverridableAtRunTime = (config: any, isSuiteLevelOverride: boolean, onErr: (result: InvalidTestOverrideResult) => void) => {
Object.keys(config).some((configKey) => {
const overrideLevel: OverrideLevel = testOverrideLevels[configKey]
if (!overrideLevel) {
// non-cypress configuration option. skip validation
return
}
// TODO: add a hook to ensure valid testing-type configuration is being set at runtime for all configuration values.
// https://github.com/cypress-io/cypress/issues/24365
if (overrideLevel === 'never' || (overrideLevel === 'suite' && !isSuiteLevelOverride)) {
onErr({
invalidConfigKey: configKey,
supportedOverrideLevel: overrideLevel,
})
}
})
}
export const validateNeedToRestartOnChange = (cachedConfig: any, updatedConfig: any) => {
const restartOnChange = {
browser: false,
server: false,
}
if (!cachedConfig || !updatedConfig) {
return restartOnChange
}
const configDiff = _.reduce<any, string[]>(cachedConfig, (result, value, key) => _.isEqual(value, updatedConfig[key]) ? result : result.concat(key), [])
restartOnChangeOptionsKeys.forEach((o) => {
if (o.requireRestartOnChange && configDiff.includes(o.name)) {
restartOnChange[o.requireRestartOnChange] = true
}
})
// devServer property is not part of the options, but we should trigger a server
// restart if we identify any change
if (!_.isEqual(cachedConfig.devServer, updatedConfig.devServer)) {
restartOnChange.server = true
}
return restartOnChange
}