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

Commit 2efe19b

Browse files
committed
feat: support base option for #4
1 parent 753f2c3 commit 2efe19b

File tree

3 files changed

+60
-3
lines changed

3 files changed

+60
-3
lines changed

README.md

+16
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,17 @@ const options = {
5656
beforeEach(mountVue(/* my Vue code */, options))
5757
```
5858

59+
* `base` - specify `<base href=...>` path. Useful to get static assets work,
60+
but might prevent relative HTTP references from working (like path to Vue.js
61+
from `../../node_modules/vue/dist/vue.js` for example)
62+
63+
```js
64+
const options = {
65+
base: '/'
66+
}
67+
beforeEach(mountVue(/* my Vue code */, options))
68+
```
69+
5970
* `html` - custom test HTML to inject instead of default one. Good
6071
place to load additional libraries, polyfills and styles.
6172

@@ -466,6 +477,11 @@ describe('Hello.vue', () => {
466477
})
467478
```
468479

480+
## FAQ
481+
482+
- If your component's static assets are not loading, you probably need
483+
to start and proxy Webpack dev server. See [issue #4](https://github.com/bahmutov/cypress-vue-unit-test/issues/4)
484+
469485
## Related info
470486

471487
- [Testing Vue web applications with Vuex data store & REST backend](https://www.cypress.io/blog/2017/11/28/testing-vue-web-application-with-vuex-data-store-and-rest-backend/)

cypress/integration/options-spec.js

+18
Original file line numberDiff line numberDiff line change
@@ -45,3 +45,21 @@ describe('Pass window HTML to use', () => {
4545
cy.window().its('Vue.version').should('be.a', 'string')
4646
})
4747
})
48+
49+
describe('Pass Vue.js url and base tag', () => {
50+
const options = {
51+
vue: 'https://unpkg.com/vue',
52+
base: '/'
53+
}
54+
55+
const component = { template, data }
56+
beforeEach(mountVue(component, options))
57+
58+
it('shows hello', () => {
59+
cy.contains('Hello Vue!')
60+
})
61+
62+
it('has version', () => {
63+
cy.window().its('Vue.version').should('be.a', 'string')
64+
})
65+
})

src/index.js

+26-3
Original file line numberDiff line numberDiff line change
@@ -53,11 +53,34 @@ const getPageHTML = options => {
5353

5454
// note: add "base" tag to force loading static assets
5555
// from the server, not from the "spec" file URL
56+
if (options.base) {
57+
if (vue.startsWith('.')) {
58+
console.error(
59+
'You are using base tag %s and relative Vue path %s',
60+
options.base,
61+
vue
62+
)
63+
console.error('the relative path might NOT work')
64+
console.error(
65+
'maybe pass Vue url using "https://unpkg.com/vue/dist/vue.js"'
66+
)
67+
}
68+
return stripIndent`
69+
<html>
70+
<head>
71+
<base href="${options.base}" />
72+
</head>
73+
<body>
74+
<div id="app"></div>
75+
<script src="${vue}"></script>
76+
</body>
77+
</html>
78+
`
79+
}
80+
5681
const vueHtml = stripIndent`
5782
<html>
58-
<head>
59-
<base href="/" />
60-
</head>
83+
<head></head>
6184
<body>
6285
<div id="app"></div>
6386
<script src="${vue}"></script>

0 commit comments

Comments
 (0)