Webpack: Pass props to component in Unit Test

Created on 24 Apr 2017  路  2Comments  路  Source: vuejs-templates/webpack

Hi!

I'm trying to create a unit test for my app based in Vue and vue-cli with webpack template.

I read Vue documentation, vue-loader, vue-cli and vue-template/webpack (an more!). When I try to make a unit test for my component, I use this.

const Constructor = Vue.extend(MyComponent)
vm = new Constructor().$mount()

like Vue-template/webpack example and like Vue official

This work fine. but the pronlem is when my component has some props. I try to pass with this

const Constructor = Vue.extend(MyComponent)
vm = new Constructor({
  myprop: 10
}).$mount()

And in my unit test, when I try to get this prop, I get undefined. How can I pass prop to my component in unit test?

Most helpful comment

I solve it with this

const Constructor = Vue.extend(MyComponent)
vm = new Constructor({
    data: {
        myProp: 10
    }
}).$mount()

or this

const Constructor = Vue.extend(MyComponent)
vm = new Constructor({
    propsData: {
        myProp: 10
    }
}).$mount()

All 2 comments

I solve it with this

const Constructor = Vue.extend(MyComponent)
vm = new Constructor({
    data: {
        myProp: 10
    }
}).$mount()

or this

const Constructor = Vue.extend(MyComponent)
vm = new Constructor({
    propsData: {
        myProp: 10
    }
}).$mount()

You can use the next package vue-test-utils

Was this page helpful?
0 / 5 - 0 ratings

Related issues

v1ar31 picture v1ar31  路  3Comments

rkrejcii picture rkrejcii  路  4Comments

nicolas-t picture nicolas-t  路  4Comments

dfdgsdfg picture dfdgsdfg  路  4Comments

connor11528 picture connor11528  路  3Comments