forked from cypress-io/cypress
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.ts
323 lines (283 loc) · 9.27 KB
/
index.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
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
/* eslint-disable no-redeclare */
/// <reference types="cypress" />
import { ComponentPublicInstance, VNodeProps, AllowedComponentProps,
ComponentCustomProps, ExtractPropTypes, ExtractDefaultPropTypes,
Component, DefineComponent, FunctionalComponent, ComputedOptions,
MethodOptions, ComponentOptionsMixin, EmitsOptions, ComponentOptionsWithObjectProps, ComponentPropsOptions, ComponentOptionsWithArrayProps, ComponentOptionsWithoutProps } from 'vue'
import { MountingOptions, VueWrapper, mount as VTUmount } from '@vue/test-utils'
import {
injectStylesBeforeElement,
StyleOptions,
ROOT_ID,
setupHooks,
} from '@cypress/mount-utils'
const DEFAULT_COMP_NAME = 'unknown'
type GlobalMountOptions = Required<MountingOptions<any>>['global']
// when we mount a Vue component, we add it to the global Cypress object
// so here we extend the global Cypress namespace and its Cypress interface
declare global {
// eslint-disable-next-line no-redeclare
namespace Cypress {
interface Cypress {
vueWrapper: VueWrapper<ComponentPublicInstance>
vue: ComponentPublicInstance
}
}
}
type CyMountOptions<Props, Data = {}> = Omit<MountingOptions<Props, Data>, 'attachTo'> & {
log?: boolean
/**
* @deprecated use vue-test-utils `global` instead
*/
extensions?: GlobalMountOptions & {
use?: GlobalMountOptions['plugins']
mixin?: GlobalMountOptions['mixins']
}
} & Partial<StyleOptions>
let initialInnerHtml = ''
Cypress.on('run:start', () => {
// `mount` is designed to work with component testing only.
// it assumes ROOT_ID exists, which is not the case in e2e.
// if the user registers a custom command that imports `cypress/vue`,
// this event will be registered and cause an error when the user
// launches e2e (since it's common to use Cypress for both CT and E2E.
// https://github.com/cypress-io/cypress/issues/17438
if (Cypress.testingType !== 'component') {
return
}
initialInnerHtml = document.head.innerHTML
Cypress.on('test:before:run', () => {
Cypress.vueWrapper?.unmount()
// @ts-ignore
const document: Document = cy.state('document')
let el = document.getElementById(ROOT_ID)
if (!el) {
throw Error(`no element found at query #${ROOT_ID}. Please use the mount utils to mount it properly`)
}
el.innerHTML = ''
document.head.innerHTML = initialInnerHtml
})
})
/**
* the types for mount have been copied directly from the VTU mount
* https://github.com/vuejs/vue-test-utils-next/blob/master/src/mount.ts
*
* If they are updated please copy and pase them again here.
*/
type PublicProps = VNodeProps & AllowedComponentProps & ComponentCustomProps;
// Class component - no props
export function mount<V>(
originalComponent: {
new (...args: any[]): V
registerHooks(keys: string[]): void
},
options?: MountingOptions<any>
): Cypress.Chainable
// Class component - props
export function mount<V, P>(
originalComponent: {
new (...args: any[]): V
props(Props: P): any
registerHooks(keys: string[]): void
},
options?: CyMountOptions<P & PublicProps>
): Cypress.Chainable
// Functional component with emits
export function mount<Props, E extends EmitsOptions = {}>(
originalComponent: FunctionalComponent<Props, E>,
options?: CyMountOptions<Props & PublicProps>
): Cypress.Chainable
// Component declared with defineComponent
export function mount<
PropsOrPropOptions = {},
RawBindings = {},
D = {},
C extends ComputedOptions = ComputedOptions,
M extends MethodOptions = MethodOptions,
Mixin extends ComponentOptionsMixin = ComponentOptionsMixin,
Extends extends ComponentOptionsMixin = ComponentOptionsMixin,
E extends EmitsOptions = Record<string, any>,
EE extends string = string,
PP = PublicProps,
Props = Readonly<ExtractPropTypes<PropsOrPropOptions>>,
Defaults = ExtractDefaultPropTypes<PropsOrPropOptions>
>(
component: DefineComponent<
PropsOrPropOptions,
RawBindings,
D,
C,
M,
Mixin,
Extends,
E,
EE,
PP,
Props,
Defaults
>,
options?: CyMountOptions<
Partial<Defaults> & Omit<Props & PublicProps, keyof Defaults>,
D
>
): Cypress.Chainable
// Component declared with no props
export function mount<
Props = {},
RawBindings = {},
D = {},
C extends ComputedOptions = {},
M extends Record<string, Function> = {},
E extends EmitsOptions = Record<string, any>,
Mixin extends ComponentOptionsMixin = ComponentOptionsMixin,
Extends extends ComponentOptionsMixin = ComponentOptionsMixin,
EE extends string = string
>(
componentOptions: ComponentOptionsWithoutProps<
Props,
RawBindings,
D
>,
options?: CyMountOptions<Props & PublicProps, D>
): Cypress.Chainable
// Component declared with { props: [] }
export function mount<
PropNames extends string,
RawBindings,
D,
C extends ComputedOptions = {},
M extends Record<string, Function> = {},
E extends EmitsOptions = Record<string, any>,
Mixin extends ComponentOptionsMixin = ComponentOptionsMixin,
Extends extends ComponentOptionsMixin = ComponentOptionsMixin,
EE extends string = string,
Props extends Readonly<{ [key in PropNames]?: any }> = Readonly<
{ [key in PropNames]?: any }
>
>(
componentOptions: ComponentOptionsWithArrayProps<
PropNames,
RawBindings,
D,
C,
M,
E,
Mixin,
Extends,
EE,
Props
>,
options?: CyMountOptions<Props & PublicProps, D>
): Cypress.Chainable
// Component declared with { props: { ... } }
export function mount<
// the Readonly constraint allows TS to treat the type of { required: true }
// as constant instead of boolean.
PropsOptions extends Readonly<ComponentPropsOptions>,
RawBindings,
D,
C extends ComputedOptions = {},
M extends Record<string, Function> = {},
E extends EmitsOptions = Record<string, any>,
Mixin extends ComponentOptionsMixin = ComponentOptionsMixin,
Extends extends ComponentOptionsMixin = ComponentOptionsMixin,
EE extends string = string
>(
componentOptions: ComponentOptionsWithObjectProps<
PropsOptions,
RawBindings,
D,
C,
M,
E,
Mixin,
Extends,
EE
>,
options?: CyMountOptions<ExtractPropTypes<PropsOptions> & PublicProps, D>
): Cypress.Chainable
// implementation
export function mount (
componentOptions: any,
options: CyMountOptions<any> = {},
) {
// TODO: get the real displayName and props from VTU shallowMount
const componentName = getComponentDisplayName(componentOptions)
const message = `<${componentName} ... />`
let logInstance: Cypress.Log
// then wait for cypress to load
return cy.then(() => {
if (options.log !== false) {
logInstance = Cypress.log({
name: 'mount',
message: [message],
})
}
// @ts-ignore
const document: Document = cy.state('document')
let el = document.getElementById(ROOT_ID)
if (!el) {
throw Error(`no element found at query #${ROOT_ID}. Please use the mount utils to mount it properly`)
}
injectStylesBeforeElement(options, document, el)
// merge the extensions with global
if (options.extensions) {
options.extensions.plugins = ([] as GlobalMountOptions['plugins'])?.concat(options.extensions.plugins || [], options.extensions.use || [])
options.extensions.mixins = ([] as GlobalMountOptions['mixins'])?.concat(options.extensions.mixins || [], options.extensions.mixin || [])
options.global = { ...options.extensions, ...options.global }
}
const componentNode = document.createElement('div')
componentNode.id = '__cy_vue_root'
el.append(componentNode)
// mount the component using VTU and return the wrapper in Cypress.VueWrapper
const wrapper = VTUmount(componentOptions, { attachTo: componentNode, ...options })
Cypress.vueWrapper = wrapper as VueWrapper<ComponentPublicInstance>
Cypress.vue = wrapper.vm as ComponentPublicInstance
return cy
.wrap(wrapper, { log: false })
.wait(1, { log: false })
.then(() => {
if (logInstance) {
logInstance.snapshot('mounted')
logInstance.end()
}
// by returning undefined we keep the previous subject
// which is the mounted component
return undefined
})
})
}
/**
* Extract the compoennt name from the object passed to mount
* @param componentOptions the compoennt passed to mount
* @returns name of the component
*/
function getComponentDisplayName (componentOptions: any): string {
if (componentOptions.name) {
return componentOptions.name
}
if (componentOptions.__file) {
const filepathSplit = componentOptions.__file.split('/')
const fileName = filepathSplit[filepathSplit.length - 1]
// remove the extension .js, .ts or .vue from the filename to get the name of the component
const baseFileName = fileName.replace(/\.(js|ts|vue)?$/, '')
// if the filename is index, then we can use the direct parent foldername, else use the name itself
return (baseFileName === 'index' ? filepathSplit[filepathSplit.length - 2] : baseFileName)
}
return DEFAULT_COMP_NAME
}
/**
* Helper function for mounting a component quickly in test hooks.
* @example
* import {mountCallback} from '@cypress/vue'
* beforeEach(mountVue(component, options))
*/
export function mountCallback (
component: any,
options: any = {},
): () => Cypress.Chainable {
return () => {
return mount(component, options)
}
}
setupHooks()