-
Notifications
You must be signed in to change notification settings - Fork 3.2k
/
index.ts
510 lines (462 loc) · 13.9 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
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
/* eslint-disable no-redeclare */
/// <reference types="cypress" />
import type {
ComponentPublicInstance,
VNodeProps,
AllowedComponentProps,
ComponentCustomProps,
ExtractPropTypes,
ExtractDefaultPropTypes,
DefineComponent,
FunctionalComponent,
ComputedOptions,
MethodOptions,
ComponentOptionsMixin,
EmitsOptions,
ComponentOptionsWithObjectProps,
ComponentPropsOptions,
ComponentOptionsWithArrayProps,
ComponentOptionsWithoutProps,
Prop,
} from 'vue'
import type { MountingOptions as VTUMountingOptions, VueWrapper } from '@vue/test-utils'
import {
getContainerEl,
setupHooks,
checkForRemovedStyleOptions,
} from '@cypress/mount-utils'
import * as _VueTestUtils from '@vue/test-utils'
const {
// We do not expose the `mount` from VueTestUtils, instead, we wrap it and expose a
// Cypress-compatible `mount` API.
mount: VTUmount,
// We do not expose shallowMount. It doesn't make much sense in the context of Cypress.
// It might be useful for people who like to migrate some Test Utils tests to Cypress,
// so if we decide it is useful to expose, just remove the next line, and it will be
// available on the `VueTestUtils` import.
shallowMount,
...VueTestUtils
} = _VueTestUtils
export { VueTestUtils }
const DEFAULT_COMP_NAME = 'unknown'
const VUE_ROOT = '__cy_vue_root'
type GlobalMountOptions = Required<VTUMountingOptions<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 MountingOptions<Props, Data = {}> = Omit<VTUMountingOptions<Props, Data>, 'attachTo'> & {
log?: boolean
/**
* @deprecated use vue-test-utils `global` instead
*/
extensions?: GlobalMountOptions & {
use?: GlobalMountOptions['plugins']
mixin?: GlobalMountOptions['mixins']
}
}
export type CyMountOptions<Props, Data = {}> = MountingOptions<Props, Data>
const cleanup = () => {
Cypress.vueWrapper?.unmount()
Cypress.$(`#${VUE_ROOT}`).remove()
;(Cypress as any).vueWrapper = null
;(Cypress as any).vue = null
}
/**
* 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.
*
* There isn't a good way to make them generic enough that we can extend them.
*
* In addition, we modify the types slightly.
*
* `MountOptions` are modifying, including some Cypress specific options like `styles`.
* The return type is different. Instead of VueWrapper, it's Cypress.Chainable<VueWrapper<...>>.
*/
type PublicProps = VNodeProps & AllowedComponentProps & ComponentCustomProps
type ComponentMountingOptions<T> = T extends DefineComponent<
infer PropsOrPropOptions,
any,
infer D,
any,
any
>
? MountingOptions<
Partial<ExtractDefaultPropTypes<PropsOrPropOptions>> &
Omit<
Readonly<ExtractPropTypes<PropsOrPropOptions>> & PublicProps,
keyof ExtractDefaultPropTypes<PropsOrPropOptions>
>,
D
> &
Record<string, any>
: MountingOptions<any>
// Class component (without vue-class-component) - no props
export function mount<V extends {}>(
originalComponent: {
new (...args: any[]): V
__vccOpts: any
},
options?: MountingOptions<any> & Record<string, any>
): Cypress.Chainable<{
wrapper: VueWrapper<ComponentPublicInstance<V>>
component: VueWrapper<ComponentPublicInstance<V>>['vm']
}>
// Class component (without vue-class-component) - props
export function mount<V extends {}, P>(
originalComponent: {
new (...args: any[]): V
__vccOpts: any
defaultProps?: Record<string, Prop<any>> | string[]
},
options?: MountingOptions<P & PublicProps> & Record<string, any>
): Cypress.Chainable<{
wrapper: VueWrapper<ComponentPublicInstance<V>>
component: VueWrapper<ComponentPublicInstance<V>>['vm']
}>
// Class component - no props
export function mount<V extends {}>(
originalComponent: {
new (...args: any[]): V
registerHooks(keys: string[]): void
},
options?: MountingOptions<any> & Record<string, any>
): Cypress.Chainable<{
wrapper: VueWrapper<ComponentPublicInstance<V>>
component: VueWrapper<ComponentPublicInstance<V>>['vm']
}>
// Class component - props
export function mount<V extends {}, P>(
originalComponent: {
new (...args: any[]): V
props(Props: P): any
registerHooks(keys: string[]): void
},
options?: MountingOptions<P & PublicProps> & Record<string, any>
): Cypress.Chainable<{
wrapper: VueWrapper<ComponentPublicInstance<V>>
component: VueWrapper<ComponentPublicInstance<V>>['vm']
}>
// Functional component with emits
export function mount<Props extends {}, E extends EmitsOptions = {}>(
originalComponent: FunctionalComponent<Props, E>,
options?: MountingOptions<Props & PublicProps> & Record<string, any>
): Cypress.Chainable<{
wrapper: VueWrapper<ComponentPublicInstance<Props>>
component: VueWrapper<ComponentPublicInstance<Props>>['vm']
}>
// 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 extends {} = ExtractDefaultPropTypes<PropsOrPropOptions>
>(
component: DefineComponent<
PropsOrPropOptions,
RawBindings,
D,
C,
M,
Mixin,
Extends,
E,
EE,
PP,
Props,
Defaults
>,
options?: MountingOptions<
Partial<Defaults> & Omit<Props & PublicProps, keyof Defaults>,
D
> &
Record<string, any>
): Cypress.Chainable<{
wrapper: VueWrapper<
InstanceType<
DefineComponent<
PropsOrPropOptions,
RawBindings,
D,
C,
M,
Mixin,
Extends,
E,
EE,
PP,
Props,
Defaults
>
>
>
component: VueWrapper<
InstanceType<
DefineComponent<
PropsOrPropOptions,
RawBindings,
D,
C,
M,
Mixin,
Extends,
E,
EE,
PP,
Props,
Defaults
>
>
>['vm']
}>
// component declared by vue-tsc ScriptSetup
export function mount<T extends DefineComponent<any, any, any, any, any>>(
component: T,
options?: ComponentMountingOptions<T>
): Cypress.Chainable<{
wrapper: VueWrapper<InstanceType<T>>
component: VueWrapper<InstanceType<T>>['vm']
}>
// Component declared with no props
export function mount<
Props = {},
RawBindings = {},
D extends {} = {},
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,
C,
M,
E,
Mixin,
Extends,
EE
>,
options?: MountingOptions<Props & PublicProps, D>
): Cypress.Chainable<{
wrapper: VueWrapper<
ComponentPublicInstance<Props, RawBindings, D, C, M, E, VNodeProps & Props>
> &
Record<string, any>
component: VueWrapper<
ComponentPublicInstance<Props, RawBindings, D, C, M, E, VNodeProps & Props>
> &
Record<string, any>['vm']
}>
// Component declared with { props: [] }
export function mount<
PropNames extends string,
RawBindings,
D extends {},
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?: MountingOptions<Props & PublicProps, D>
): Cypress.Chainable<{
wrapper: VueWrapper<ComponentPublicInstance<Props, RawBindings, D, C, M, E>>
component: VueWrapper<ComponentPublicInstance<Props, RawBindings, D, C, M, E>>['vm']
}>
// 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 extends {},
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?: MountingOptions<ExtractPropTypes<PropsOptions> & PublicProps, D>
): Cypress.Chainable<{
wrapper: VueWrapper<
ComponentPublicInstance<
ExtractPropTypes<PropsOptions>,
RawBindings,
D,
C,
M,
E,
VNodeProps & ExtractPropTypes<PropsOptions>
>
>
component: VueWrapper<
ComponentPublicInstance<
ExtractPropTypes<PropsOptions>,
RawBindings,
D,
C,
M,
E,
VNodeProps & ExtractPropTypes<PropsOptions>
>
>['vm']
}>
/**
* Mounts a component and returns an object containing the component and VueWrapper
* @param componentOptions
* @param options
* @returns {Cypress.Chainable<{wrapper: VueWrapper<T>, component: T}>}
* @see {@link https://on.cypress.io/mounting-vue} for more details.
* @example
* import { mount } from '@cypress/vue'
* import { Stepper } from './Stepper.vue'
*
* it('mounts', () => {
* cy.mount(Stepper)
* cy.get('[data-cy=increment]').click()
* cy.get('[data-cy=counter]').should('have.text', '1')
* })
*/
export function mount (componentOptions: any, options: any = {}) {
checkForRemovedStyleOptions(options)
// Remove last mounted component if cy.mount is called more than once in a test
cleanup()
// then wait for cypress to load
return cy.then(() => {
// @ts-ignore
const document: Document = cy.state('document')
const el = getContainerEl()
// 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 = 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
.wait(1, { log: false })
.then(() => {
if (options.log !== false) {
// TODO: get the real displayName and props from VTU shallowMount
const message = `<${getComponentDisplayName(componentOptions)} ... />`
Cypress.log({
name: 'mount',
message: [message],
})
}
const returnVal = {
wrapper,
component: wrapper.vm,
}
return new Proxy(Object.create(returnVal), {
get (obj, prop) {
// throw an error if it looks like the caller is trying to call a method on the VueWrapper that was originally returned
if (Reflect.get(wrapper, prop)) {
// @ts-expect-error - internal API
Cypress.utils.throwErrByPath('mount.vue_yielded_value')
}
return Reflect.get(obj, prop)
},
})
})
})
}
/**
* Extract the component name from the object passed to mount
* @param componentOptions the component 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] ?? DEFAULT_COMP_NAME
// 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))
*
* Removed as of Cypress 11.0.0.
* @see https://on.cypress.io/migration-11-0-0-component-testing-updates
*/
export function mountCallback (
component: any,
options: any = {},
) {
return () => {
// @ts-expect-error - undocumented API
Cypress.utils.throwErrByPath('mount.mount_callback')
}
}
// Side effects from "import { mount } from '@cypress/<my-framework>'" are annoying, we should avoid doing this
// by creating an explicit function/import that the user can register in their 'component.js' support file,
// such as:
// import 'cypress/<my-framework>/support'
// or
// import { registerCT } from 'cypress/<my-framework>'
// registerCT()
// Note: This would be a breaking change
setupHooks(cleanup)