[Vuejs]-How to set or test this.$parent in Vuejs unit testing?

0👍

Why not import you categories config file directly into your component?

import Categories from '../config/categories'

then your data method can directly access it:

data () { return { categories: Categories }}

You’ll find that much easier to test

0👍

yes, thanks you. I changed. I run test then it happens error other

Cannot find module '../../config/categories' from 'mycomponet.vue'.

Although. I run project on browser just working well.
How to fix it. Thanks you very much

0👍

For Testing , you can set mock data to escape undefined error while testing . But it is not standard solution …..

it(‘check component is a button’,() => {
   const wrapper = shallow(FormSearch);
   let mockCategories = { // mock category data }
   wrapper.$parent = {
     categories: mockCategories
   }
   expect(wrapper.contains(‘button’)).toBe(true);
});

0👍

Try this approach:

const Parent = {
  data: () => ({
    val: true
  }),
  template: '<div />'
}

const wrapper = shallowMount(TestComponent, {
  parent: Parent
})

Leave a comment