-
Notifications
You must be signed in to change notification settings - Fork 3.1k
/
misc.ts
102 lines (87 loc) · 2.53 KB
/
misc.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
import _ from 'lodash'
import Promise from 'bluebird'
import $Command from '../../cypress/command'
import $dom from '../../dom'
import $errUtils from '../../cypress/error_utils'
import type { Log } from '../../cypress/log'
interface InternalWrapOptions extends Partial<Cypress.Loggable & Cypress.Timeoutable> {
_log?: Log
timeout: number
}
export default (Commands, Cypress, cy, state) => {
Commands.addAll({ prevSubject: 'optional' }, {
end () {
return null
},
})
Commands.addAll({
noop (arg) {
return arg
},
log (msg, ...args) {
// https://github.com/cypress-io/cypress/issues/8084
// The return value of cy.log() corrupts the command stack, so cy.then() returned the wrong value
// when cy.log() is used inside it.
// The code below restore the stack when cy.log() is injected in cy.then().
if (state('current').get('injected')) {
const restoreCmdIndex = state('index') + 1
cy.queue.insert(restoreCmdIndex, $Command.create({
args: [state('subject')],
name: 'log-restore',
fn: (subject) => subject,
}))
state('index', restoreCmdIndex)
}
Cypress.log({
end: true,
snapshot: true,
message: [msg, ...args],
consoleProps () {
return {
message: msg,
args,
}
},
})
return null
},
wrap (arg, userOptions: Partial<Cypress.Loggable & Cypress.Timeoutable> = {}) {
const options: InternalWrapOptions = _.defaults({}, userOptions, {
log: true,
timeout: Cypress.config('defaultCommandTimeout'),
})
// we'll handle the timeout ourselves
cy.clearTimeout()
if (options.log !== false) {
options._log = Cypress.log({
message: arg,
timeout: options.timeout,
})
if ($dom.isElement(arg)) {
options._log!.set({ $el: arg })
}
}
return Promise.resolve(arg)
.timeout(options.timeout)
.catch(Promise.TimeoutError, () => {
$errUtils.throwErrByPath('wrap.timed_out', {
args: { timeout: options.timeout },
})
})
.catch((err) => {
$errUtils.throwErr(err, {
onFail: options._log,
})
})
.then((subject) => {
const resolveWrap = () => {
return cy.verifyUpcomingAssertions(subject, options, {
onRetry: resolveWrap,
})
.return(subject)
}
return resolveWrap()
})
},
})
}