Skip to content
This repository was archived by the owner on Dec 12, 2020. It is now read-only.

Commit 0a16be5

Browse files
authored
fix: add types for Cypress.vue property (#354)
and update constructor type for Vue components
1 parent dee2464 commit 0a16be5

File tree

5 files changed

+43
-1
lines changed

5 files changed

+43
-1
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -596,6 +596,7 @@ Spec | Description
596596
<!-- prettier-ignore-start -->
597597
Spec | Description
598598
--- | ---
599+
[access-component](cypress/component/advanced/access-component) | Access the mounted component directly from test
599600
[i18n](cypress/component/advanced/i18n) | Testing component that uses [Vue I18n](https://kazupon.github.io/vue-i18n/) plugin
600601
[mocking-axios](cypress/component/advanced/mocking-axios) | Mocking 3rd party module imports, like `axios` for fetching data
601602
[mocking-components](cypress/component/advanced/mocking-components) | Mocking locally registered child components during tests
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# Access the component
2+
3+
If you need to access the mounted component from test, a reference is available at `Cypress.vue` after the `mount` finishes (asynchronously).
4+
5+
```js
6+
mount(...)
7+
.then(() => {
8+
Cypress.vue.<prop> = ...
9+
// or call a method
10+
Cypress.vue.<method()
11+
})
12+
// then check the UI
13+
cy.contains(...)
14+
```
15+
16+
See component [Message.vue](Message.vue) and [message-spec.js](message-spec.js)

cypress/component/basic/message-spec.js renamed to cypress/component/advanced/access-component/message-spec.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
/// <reference path="../../../../dist/index.d.ts" />
12
import Message from './Message.vue'
23
import { mount } from 'cypress-vue-unit-test'
34

src/index.ts

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ const resetStoreVM = (Vue, { store }) => {
119119
* ^^^^^ this type
120120
* mount(Hello)
121121
*/
122-
type VueComponent = Vue.ComponentOptions<any>
122+
type VueComponent = Vue.ComponentOptions<any> | Vue.VueConstructor
123123

124124
/**
125125
* Options to pass to the component when creating it, like
@@ -286,6 +286,28 @@ interface MountOptions {
286286
*/
287287
type MountOptionsArgument = Partial<ComponentOptions & MountOptions>
288288

289+
// when we mount a Vue component, we add it to the global Cypress object
290+
// so here we extend the global Cypress namespace and its Cypress interface
291+
declare global {
292+
// eslint-disable-next-line no-redeclare
293+
namespace Cypress {
294+
interface Cypress {
295+
/**
296+
* Mounted Vue instance is available under Cypress.vue
297+
* @memberof Cypress
298+
* @example
299+
* mount(Greeting)
300+
* .then(() => {
301+
* Cypress.vue.message = 'Hello There'
302+
* })
303+
* // new message is displayed
304+
* cy.contains('Hello There').should('be.visible')
305+
*/
306+
vue: Vue
307+
}
308+
}
309+
}
310+
289311
/**
290312
* Direct Vue errors to the top error handler
291313
* where they will fail Cypress test
@@ -352,6 +374,7 @@ export const mount = (
352374
// https://github.com/bahmutov/cypress-vue-unit-test/issues/313
353375
if (
354376
Cypress._.isPlainObject(component) &&
377+
// @ts-ignore
355378
Cypress._.isFunction(component.render)
356379
) {
357380
// @ts-ignore
@@ -412,6 +435,7 @@ export const mount = (
412435
// create root Vue component
413436
// and make it accessible via Cypress.vue
414437
if (isConstructor(component)) {
438+
// @ts-ignore
415439
const Cmp = Vue.extend(component)
416440
// @ts-ignore
417441
Cypress.vue = new Cmp(props).$mount(componentNode)

0 commit comments

Comments
 (0)