【问题标题】:unable to get vuex store getters data in vuejs using jest无法使用 jest 在 vuejs 中获取 vuex 存储 getters 数据
【发布时间】:2020-12-08 21:57:12
【问题描述】:

我正在尝试使用 jest 测试 vuex 商店的 getters 方法。但是结果是不确定的。 下面是我正在使用的代码,

storeConfigurations.ts
---------------------
const state = {
    update: false
}

const getters = {

  [types.GET_UPDATE_VALUE]: (state: { update: any }) => {
        return state.update;
    }

}

types.ts
--------
export const GET_UPDATE_VALUE = 'GET/update'

尝试在 updatedValue() 方法中测试 store getters 方法,结果未定义。

update.vue
===========

  <div class="updateCss">
      <customList :items="updatedValue"></customList>
    </div>
    
export default class UpdateTest extends Vue {

get updatedValue() {
        
const updatedValue = this.$store.getters[
      types.GET_UPDATE_VALUE
    ];      
}


update.spec.ts
==============

import Vuex from 'vuex' 
import storeConfigurations from '@/store/modules/storeDetails'
const localVue = createLocalVue()
localVue.use(Vuex)

 describe('Update Component TestSuite', () => {
 
 beforeEach(() => {
    let store
    let updateWrapper
     store = new Vuex.Store({
      modules: {
        storeConfigurations: {
          state: { update:false},
          getters: {
            updatedValue: jest.fn()
            
         }
      }
      }
    })

     updateWrapper = shallowMount(UpdateTest , {
      store,
      localVue
        
 }

我在这里尝试从 store 中获取更新的值,但是在模拟 store 时我变得未定义。

【问题讨论】:

    标签: jestjs vuex store getter


    【解决方案1】:

    通过在模块内提供带有状态变量的 getter 解决了问题。

    【讨论】:

      猜你喜欢
      • 2019-10-24
      • 1970-01-01
      • 2022-12-11
      • 2018-11-10
      • 1970-01-01
      • 1970-01-01
      • 2021-10-03
      • 1970-01-01
      • 2021-05-03
      相关资源
      最近更新 更多