1

I am just getting start with Vue Testing with the help of Jest and Vue Test Utils.

I am trying to mount a component named DateComponent.vue in my Jest test file, but when running the test it throws an error

TypeError: Cannot read property 'key' of undefined

Mounting other Components seems to work fine and I can test them without problems, but this one seems to have some issues.

    <template>
  <v-menu
      v-model="dateMenu"
      :close-on-content-click="false"
      :nudge-right="40"
      transition="scale-transition"
      offset-y
      min-width="290px"
    >
      <template v-slot:activator="{ on }">
        <v-text-field
          v-model="value[field.key]"
          label=""
          readonly
          v-on="on"
        ></v-text-field>
      </template>
      <v-date-picker v-model="value[field.key]" @input="dateMenu = false" locale='de-de'></v-date-picker>
    </v-menu>
</template>

<script>
export default {
  props: ['value', 'field'],
  data: () => ({
    dateMenu: false
  }),
  mounted: function () {

  },
  methods: {

  },
  filters: {

  }
}
</script>

And thats how I am mounting the component:

import { mount } from '@vue/test-utils'
import DateComponent from './components/DateComponent'


const wrapper = mount(DateComponent)

The issue seems to be within the <v-text-field> and <v-date-picker>, specifically the v-model="value[field.key]" but I am not experienced enough to figure out whats going on.

Thanks a lot

EDIT

How I fixed it:

const wrapper = mount(TestComponent, {
  propsData: {
    field: {
    },
    value: {
    }
  }
})

When I mounted the TestComponent, I set its propsData and data to empty data

1
  • Actually, you can stub <v-text-field> & <v-date-picker> where you don't need worry about nested components. Check out this link for more info Commented Dec 4, 2020 at 18:26

1 Answer 1

1

Your problem is when you're trying to acces v-model="value[field.key]", probably you didn't pass the field prop to your component, so, it will be undefined and the cause of your error.

Additionally, you should'n bind v-model in a prop, if it's a static value, use :value instead

Sign up to request clarification or add additional context in comments.

2 Comments

Hey there, thanks for the answer! How can I pass the field prop to the component?
@Trivo You can pass a second parameter in your mount method, an object named propsData containing the props that you need. Read more here! vue-test-utils.vuejs.org/api/options.html#propsdata

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.