Skip to content

Commit 781abf7

Browse files
committed
feat: support compiling inline templates
1 parent 32857f2 commit 781abf7

File tree

5 files changed

+27
-2
lines changed

5 files changed

+27
-2
lines changed

src/lib/compile-template.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
// @flow
2+
3+
import { compileToFunctions } from 'vue-template-compiler'
4+
5+
export function compileTemplate (component: Component) {
6+
Object.assign(component, compileToFunctions(component.template))
7+
}

src/lib/create-instance.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import addProvide from './add-provide'
99
import { stubComponents } from './stub-components'
1010
import { throwError } from './util'
1111
import cloneDeep from 'lodash/cloneDeep'
12+
import { compileTemplate } from './compile-template'
1213

1314
export default function createConstructor (component: Component, options: Options): Component {
1415
const vue = options.localVue || Vue
@@ -37,6 +38,10 @@ export default function createConstructor (component: Component, options: Option
3738
stubComponents(component, options.stubs)
3839
}
3940

41+
if (!component.render && component.template && !component.functional) {
42+
compileTemplate(component)
43+
}
44+
4045
const Constructor = vue.extend(component)
4146

4247
if (options.intercept) {

src/mount.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,11 @@ Vue.config.productionTip = false
1212

1313
export default function mount (component: Component, options: Options = {}): VueWrapper {
1414
if (!window) {
15-
throwError('window is undefined, vue-test-utils needs to be run in a browser environment.\n You can run the tests in node using JSDOM')
15+
throwError(
16+
'window is undefined, vue-test-utils needs to be run in a browser environment.\n' +
17+
'You can run the tests in node using jsdom + jsdom-global.\n' +
18+
'See https://vue-test-utils.vuejs.org/en/guides/general-tips.html for more details.'
19+
)
1620
}
1721

1822
const componentToMount = options.clone === false ? component : cloneDeep(component)

test/unit/setup/mocha.setup.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1+
require('jsdom-global')()
2+
13
const chai = require('chai')
24
const sinon = require('sinon')
3-
require('jsdom-global')()
45

56
global.expect = chai.expect
67
global.sinon = sinon

test/unit/specs/mount.spec.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,4 +65,12 @@ describe('mount', () => {
6565

6666
expect(() => mount(compileToFunctions('<div />'))).to.throw().with.property('message', message)
6767
})
68+
69+
it('compiles inline templates', () => {
70+
const wrapper = mount({
71+
template: `<div>foo</div>`
72+
})
73+
expect(wrapper.vm).to.be.an('object')
74+
expect(wrapper.html()).to.equal(`<div>foo</div>`)
75+
})
6876
})

0 commit comments

Comments
 (0)