使用Jest进行单元测试。 TypeError:无法读取未定义的属性“ getters”

时间:2020-03-07 12:21:46

标签: unit-testing vue.js jestjs vue-test-utils

我有一个要测试的组件,该组件使用Vuex。

components / Main / Header.vue

<template>
  <v-container fluid grid-list-xl>
    <v-card flat class="header" color="transparent">
      <v-layout align-center justify-start row fill-height class="content">
        <v-flex xs5>
          <v-img :src="avatar" class="avatar" aspect-ratio="1" contain></v-img>
        </v-flex>
        <v-flex xs7>
          <div class="main-text font-weight-black">
            WELCOME
          </div>
          <div class="sub-text">
            {{currentLocation.description}}
          </div>
          <div @click="showStory" class="show-more font-weight-bold"> 
            Explore More
          </div>
        </v-flex>
      </v-layout>
    </v-card>
  </v-container>
</template>

<script>
  import avatar from '../../assets/images/home/avatar.png';

  export default {
    name: 'Header',
    computed: {
      currentLocation() {
        return this.$store.getters.getCurrentLocation;
      },
      avatar() {
        return avatar;
      }
    },
    methods: {
      showStory() {
        this.$router.push( { name: 'Stories',  params: { name: 'Our Story' } });
      }
    }
  }
</script>

并来自 /test/unit/components/Main/Header.spec.js

import Vuex from 'vuex'
import { shallowMount, createLocalVue } from "@vue/test-utils"
import Header from "@/components/Main/Header.vue"

const localVue = createLocalVue()
localVue.use(Vuex)

const store = new Vuex.Store({
  state: {
    currentLocation: {
      name: 'this is a name',
      description: "lorem ipsum",
      latitude: '1.123123',
      longitude: '103.123123',
      radius: '5000'
    }
  },
  getters: {
    getCurrentLocation: (state) => state.currentLocation
  }
})

describe('Header', () => {

  const wrapper = shallowMount(Header, {
    store,
    localVue
  })

  it('should render a computed property currentLocation', () => {
    expect(Header.computed.currentLocation()).toBe(store.getters.getCurrentLocation)
  });

});

我收到的错误来自计算的属性 currentLocation TypeError: Cannot read property 'getters' of undefined

0 个答案:

没有答案