-
Notifications
You must be signed in to change notification settings - Fork 5
/
index.js
160 lines (134 loc) · 4.32 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
/// <reference types="cypress" />
const { loggable } = require('./loggable')
module.exports = { loggable }
const isJquery = obj =>
!!(obj && obj.jquery && Cypress._.isFunction(obj.constructor))
const traversals = "find filter not children eq closest first last next nextAll nextUntil parent parents parentsUntil prev prevAll prevUntil siblings".split(" ")
/**
* Patch the provided jQuery collection to add a `.selector` property to all traversal methods just
* like Cypress does in https://github.com/cypress-io/cypress/blob/13ebb9779d21238cae4da4b63cf6230da4a5341d/packages/driver/src/cy/commands/traversals.coffee#L50
* This patch allows a failure to log an error message that includes the last selector used when
* failing to find an element. A nice touch!
* @param {JQueryStatic} $el
*/
const patchJQueryForSelectorProperty = ($el) => {
traversals.forEach(traversal => {
const originalFn = $el.fn[traversal]
$el.fn[traversal] = function (...args) {
const ret = originalFn.apply(this, args)
ret.selector = Cypress._.chain(args)
.reject(Cypress._.isFunction)
.reject(Cypress._.isObject)
.reject(a => a == null)
.value()
.join(', ')
return ret
}
})
}
patchJQueryForSelectorProperty(Cypress.$)
const getElements = $el => {
if (!$el && !$el.length) {
return
}
$el = $el.toArray()
if ($el.length === 1) {
return $el[0]
} else {
return $el
}
}
/**
* Format the argument according to its type
* @param {any} arg
*/
function formatArg (arg) {
switch (typeof arg) {
case 'function':
return arg.name || 'function'
default:
return JSON.stringify(arg)
}
}
Cypress.Commands.add('pipe', { prevSubject: 'optional' }, (subject, fn, options = { }) => {
// Support https://github.com/NicholasBoll/cypress-pipe/issues/22
if (!subject) {
subject = cy.state('withinSubject') || Cypress.$('body')
}
// if (isJquery(subject)) {
// patchJQueryForSelectorProperty(subject);
// }
const getEl = (value) => isJquery(value) ? value : isJquery(subject) ? subject : undefined
const now = performance.now()
let isCy = false
Cypress._.defaults(options, {
log: true,
timeout: Cypress.config('defaultCommandTimeout'),
})
if (options.log) {
options._log = Cypress.log({
message: (fn.displayName || fn.name || undefined) + (fn.__args ? `(${fn.__args.map(formatArg).join(', ')})` : ''),
$el: getEl(subject), // start the $el with the subject
})
}
const getConsoleProps = (value) => () => ({
Command: 'pipe',
Subject: subject,
Function: fn.displayName || fn.name || undefined,
Arguments: fn.__args || [],
Contents: fn.toString(),
Yielded: isJquery(value) ? getElements(value) : value,
Elements: isJquery(value) ? value.length : undefined,
Duration: performance.now() - now,
})
const getValue = () => {
const value = fn(subject)
isCy = cy.isCy(value)
const actualValue = isCy ? subject : value
if (options._log) {
if (isCy) {
// If a Cypress command was detected, handle snapshots here
options._log.snapshot('before', { next: 'after' })
value.then((val) => {
// Set the element to the value for the second snapshot
options._log.set({
$el: getEl(val),
consoleProps: getConsoleProps(val)
})
options._log.snapshot()
})
} else {
// If Cypress is not detected, snapshot at the very end
options._log.set({
$el: getEl(value),
consoleProps: getConsoleProps(value)
})
}
}
return actualValue
}
// retryValue will automatically retry piped functions that temporarily return errors
const retryValue = () => {
return Cypress.Promise.try(getValue).catch(err => {
options.error = err
return cy.retry(retryValue, options)
})
}
const resolveValue = () => {
return Cypress.Promise.try(retryValue).then(value => {
return cy.verifyUpcomingAssertions(value, options, {
onRetry: resolveValue,
})
})
}
return resolveValue().then((value) => {
if (options._log) {
if (!isCy) {
// For pure functions, this is the only safe place to snapshot (guaranteed only happens once)
options._log.snapshot()
}
options._log.end()
}
return value
})
})