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

Commit e0fa815

Browse files
authored
feat: allow options when using extensions in tests (#342)
```js const use = [ MyPlugin, // this plugin does not need options [MyPluginWithOptions, { label: 'testing' }], // this plugin needs options ] // extend Vue with plugins const extensions = { use, } mount({}, { extensions }) ```
1 parent e279fe5 commit e0fa815

File tree

7 files changed

+102
-23
lines changed

7 files changed

+102
-23
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -588,6 +588,7 @@ describe('HelloWorld component', () => {
588588
Spec | Description
589589
--- | ---
590590
[Hello](cypress/component/basic/hello) | Testing examples from Vue2 cookbook
591+
[Plugins](cypress/component/basic/plugins) | Loading additional plugins
591592
<!-- prettier-ignore-end -->
592593

593594
### Advanced examples

cypress/component/basic/plugin-spec.js

Lines changed: 0 additions & 21 deletions
This file was deleted.

cypress/component/basic/MyPlugin.js renamed to cypress/component/basic/plugins/MyPlugin.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// https://vuejs.org/v2/guide/plugins.html
2+
// https://alligator.io/vuejs/creating-custom-plugins/
23
export const MyPlugin = {
3-
install(Vue, options) {
4+
install(Vue) {
45
// 1. add global method or property
56
Vue.aPluginMethod = function () {
67
return 'foo'
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// https://vuejs.org/v2/guide/plugins.html
2+
// https://alligator.io/vuejs/creating-custom-plugins/
3+
export const MyPluginWithOptions = {
4+
install(Vue, options) {
5+
if (!options) {
6+
throw new Error('MyPlugin is missing options!')
7+
}
8+
9+
// this method uses options argument
10+
Vue.anotherPluginMethod = function () {
11+
return options.label
12+
}
13+
},
14+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# plugins
2+
3+
Spec file [plugin-spec.js](plugin-spec.js) shows how to load plugins while doing testing. The plugins might require options, which you can pass using array syntax
4+
5+
```js
6+
const use = [
7+
MyPlugin, // this plugin does not need options
8+
[MyPluginWithOptions, { label: 'testing' }], // this plugin needs options
9+
]
10+
11+
// extend Vue with plugins
12+
const extensions = {
13+
use,
14+
}
15+
16+
mount({}, { extensions })
17+
```
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
/// <reference types="cypress" />
2+
import { MyPlugin } from './MyPlugin'
3+
import { MyPluginWithOptions } from './MyPluginWithOptions'
4+
import { mount, mountCallback } from 'cypress-vue-unit-test'
5+
6+
describe('Single component mount', () => {
7+
it('has the plugin', () => {
8+
const use = [MyPlugin]
9+
10+
// extend Vue with plugins
11+
const extensions = {
12+
use,
13+
}
14+
15+
mount({}, { extensions })
16+
17+
cy.window().its('Vue').invoke('aPluginMethod').should('equal', 'foo')
18+
})
19+
})
20+
21+
describe('Custom plugin MyPlugin', () => {
22+
const use = [MyPlugin]
23+
24+
// extend Vue with plugins
25+
const extensions = {
26+
use,
27+
}
28+
// use "mountCallback" to register the plugins
29+
beforeEach(mountCallback({}, { extensions }))
30+
31+
it('registers global method on Vue instance', () => {
32+
cy.window().its('Vue').its('aPluginMethod').should('be.a', 'function')
33+
})
34+
35+
it('can call this global function', () => {
36+
cy.window().its('Vue').invoke('aPluginMethod').should('equal', 'foo')
37+
})
38+
})
39+
40+
describe('Plugins with options', () => {
41+
it('passes options', () => {
42+
const use = [
43+
MyPlugin, // this plugin does not need options
44+
[MyPluginWithOptions, { label: 'testing' }], // this plugin needs options
45+
]
46+
47+
// extend Vue with plugins
48+
const extensions = {
49+
use,
50+
}
51+
52+
mount({}, { extensions })
53+
54+
// first plugin works
55+
cy.window().its('Vue').invoke('aPluginMethod').should('equal', 'foo')
56+
// second plugin works
57+
cy.window()
58+
.its('Vue')
59+
.invoke('anotherPluginMethod')
60+
.should('equal', 'testing')
61+
})
62+
})

src/index.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,12 @@ const installPlugins = (Vue, options) => {
4848
Cypress._.get(options, 'extensions.plugins')
4949
if (Cypress._.isArray(plugins)) {
5050
plugins.forEach((plugin) => {
51-
Vue.use(plugin)
51+
if (Array.isArray(plugin)) {
52+
const [aPlugin, options] = plugin
53+
Vue.use(aPlugin, options)
54+
} else {
55+
Vue.use(plugin)
56+
}
5257
})
5358
}
5459
}

0 commit comments

Comments
 (0)