forked from cypress-io/cypress
-
Notifications
You must be signed in to change notification settings - Fork 0
/
aliases.ts
124 lines (93 loc) · 2.98 KB
/
aliases.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
import _ from 'lodash'
import type { $Cy } from '../cypress/cy'
import $errUtils from '../cypress/error_utils'
const aliasRe = /^@.+/
const aliasDisplayRe = /^([@]+)/
const requestXhrRe = /\.request$/
const reserved = ['test', 'runnable', 'timeout', 'slow', 'skip', 'inspect']
const aliasDisplayName = (name) => {
return name.replace(aliasDisplayRe, '')
}
// eslint-disable-next-line @cypress/dev/arrow-body-multiline-braces
export const create = (cy: $Cy) => ({
addAlias (ctx, aliasObj) {
const { alias, subject } = aliasObj
const aliases = cy.state('aliases') || {}
aliases[alias] = aliasObj
cy.state('aliases', aliases)
const remoteSubject = cy.getRemotejQueryInstance(subject)
ctx[alias] = remoteSubject ?? subject
},
getAlias (name, cmd, log) {
const aliases = cy.state('aliases') || {}
// bail if the name doesnt reference an alias
if (!aliasRe.test(name)) {
return
}
const alias = aliases[name.slice(1)]
// slice off the '@'
if (!alias) {
this.aliasNotFoundFor(name, cmd, log)
}
return alias
},
// below are public because its expected other commands
// know about them and are expected to call them
getNextAlias () {
const next = cy.state('current').get('next')
if (next && (next.get('name') === 'as')) {
return next.get('args')[0]
}
},
validateAlias (alias: string) {
if (!_.isString(alias)) {
$errUtils.throwErrByPath('as.invalid_type')
}
if (aliasDisplayRe.test(alias)) {
$errUtils.throwErrByPath('as.invalid_first_token', {
args: {
alias,
suggestedName: alias.replace(aliasDisplayRe, ''),
},
})
}
if (_.isEmpty(alias)) {
$errUtils.throwErrByPath('as.empty_string')
}
if (reserved.includes(alias)) {
$errUtils.throwErrByPath('as.reserved_word', { args: { alias } })
}
return null
},
aliasNotFoundFor (name, cmd, log) {
const availableAliases = cy.state('aliases')
? _.keys(cy.state('aliases'))
: []
let displayName
// throw a very specific error if our alias isnt in the right
// format, but its word is found in the availableAliases
if (!aliasRe.test(name) && availableAliases.includes(name)) {
displayName = aliasDisplayName(name)
$errUtils.throwErrByPath('alias.invalid', {
onFail: log,
args: { name, displayName },
})
}
cmd = cmd ?? ((log && log.get('name')) || cy.state('current').get('name'))
displayName = aliasDisplayName(name)
const errPath = availableAliases.length
? 'alias.not_registered_with_available'
: 'alias.not_registered_without_available'
$errUtils.throwErrByPath(errPath, {
onFail: log,
args: { cmd, displayName, availableAliases: availableAliases.join(', ') },
})
},
getXhrTypeByAlias (alias) {
if (requestXhrRe.test(alias)) {
return 'request'
}
return 'response'
},
})
export interface IAliases extends ReturnType<typeof create> {}