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

Commit 022be78

Browse files
committed
feat: pass custom Vue path or url, close #1
1 parent 4853cf5 commit 022be78

File tree

5 files changed

+117
-11
lines changed

5 files changed

+117
-11
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,4 @@ node_modules/
22
.DS_Store
33
npm-debug.log
44
cypress/videos/
5+
cypress/screenshots

README.md

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,48 @@ npm install --save-dev cypress-vue-unit-test
2424

2525
## Use
2626

27+
Before each test, inject your component to test
28+
29+
```js
30+
const mountVue = require('cypress-vue-unit-test')
31+
describe('My Vue', () => {
32+
beforeEach(mountVue(/* my Vue code */, /* options */))
33+
it('renders', () => {
34+
// Any Cypress command
35+
// Cypress.vue is the mounted component reference
36+
})
37+
})
38+
```
39+
40+
See examples below for details.
41+
42+
### Options
43+
44+
See [cypress/integration/options-spec.js](cypress/integration/options-spec.js)
45+
for examples of options.
46+
47+
* `vue` - path or URL to the Vue library to load. By default, will
48+
try to load `../node_modules/vue/dist/vue.js`, but you can pass your
49+
own path or URL.
50+
51+
```js
52+
const options = {
53+
vue: 'https://unpkg.com/vue'
54+
}
55+
beforeEach(mountVue(/* my Vue code */, options))
56+
```
57+
58+
* `html` - custom test HTML to inject instead of default one. Good
59+
place to load additional libraries, polyfills and styles.
60+
61+
```js
62+
const vue = '../node_modules/vue/dist/vue.js'
63+
const options = {
64+
html: `<div id="app"></div><script src="${vue}"></script>`
65+
}
66+
beforeEach(mountVue(/* my Vue code */, options))
67+
```
68+
2769
### The intro example
2870

2971
Take a look at the first Vue v2 example:
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
const mountVue = require('../..')
2+
3+
const template = `
4+
<div id="app">
5+
{{ message }}
6+
</div>
7+
`
8+
9+
const data = {
10+
message: 'Hello Vue!'
11+
}
12+
13+
/* eslint-env mocha */
14+
describe('Pass Vue.js url', () => {
15+
const options = {
16+
vue: 'https://unpkg.com/vue'
17+
}
18+
19+
const component = { template, data }
20+
beforeEach(mountVue(component, options))
21+
22+
it('shows hello', () => {
23+
cy.contains('Hello Vue!')
24+
})
25+
26+
it('has version', () => {
27+
cy.window().its('Vue.version').should('be.a', 'string')
28+
})
29+
})
30+
31+
describe('Pass window HTML to use', () => {
32+
const vue = '../node_modules/vue/dist/vue.js'
33+
const options = {
34+
html: `<div id="app"></div><script src="${vue}"></script>`
35+
}
36+
37+
const component = { template, data }
38+
beforeEach(mountVue(component, options))
39+
40+
it('shows hello', () => {
41+
cy.contains('Hello Vue!')
42+
})
43+
44+
it('has version', () => {
45+
cy.window().its('Vue.version').should('be.a', 'string')
46+
})
47+
})

package.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,5 +93,8 @@
9393
"Cypress",
9494
"cy"
9595
]
96+
},
97+
"dependencies": {
98+
"common-tags": "1.6.0"
9699
}
97100
}

src/index.js

Lines changed: 24 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/* global Cypress, cy */
1+
const { stripIndent } = require('common-tags')
22

33
// having weak reference to styles prevents garbage collection
44
// and "losing" styles when the next test starts
@@ -42,20 +42,33 @@ function deleteCachedConstructors (component) {
4242
Cypress._.values(component.components).forEach(deleteConstructor)
4343
}
4444

45-
const vueHtml = `
46-
<html>
47-
<head></head>
48-
<body>
49-
<script src="../node_modules/vue/dist/vue.js"></script>
50-
<div id="app"></div>
51-
</body>
52-
</html>
53-
`
45+
const getVuePath = options =>
46+
options.vue || options.vuePath || '../node_modules/vue/dist/vue.js'
5447

55-
const mountVue = component => () => {
48+
const getPageHTML = options => {
49+
if (options.html) {
50+
return options.html
51+
}
52+
const vue = getVuePath(options)
53+
const vueHtml = stripIndent`
54+
<html>
55+
<head></head>
56+
<body>
57+
<div id="app"></div>
58+
<script src="${vue}"></script>
59+
</body>
60+
</html>
61+
`
62+
return vueHtml
63+
}
64+
65+
const mountVue = (component, options = {}) => () => {
5666
cy.document().then(document => {
67+
const vueHtml = getPageHTML(options)
5768
document.write(vueHtml)
5869
document.close()
70+
console.log('wrote html')
71+
console.log(vueHtml)
5972
})
6073
cy
6174
.window()

0 commit comments

Comments
 (0)