用Jest测试NUXT.js和Vue.js应用程序。获取“在mapState()中找不到[[vuex]]模块名称空间”和“ [[vuex]]未知操作类型”

问题描述 投票:0回答:1

尽管我了解NUXT会自动进行命名间隔。因此,我无法在任何测试模块中测试或引用商店。谁能给我小费?也许我可以在Nuxt应用程序中编辑namespacing属性?

以下是组件,存储和测试的代码。

ButtonComponent.vue:

<template>
  <v-container>
    <v-btn @buttonClick v-model="value"></v-btn>
  </v-container>
</template>

<script>
import { mapState, mapActions } from 'vuex'

export default {
  data: {
      return {
          value: 25
      }
  }
  methods: {
    buttonClick(event) {
      this.$store.dispatch('buttonComponent/setNewValue', valuePassedIn)
    },
  },
}
</script>

<style scoped></style>

buttonComponent.spec.js:

import Component from '../../Component'
import { mount, createLocalVue } from '@vue/test-utils'
import expect from 'expect'
import Vue from 'vue'
import Vuex from 'vuex'
import Vuetify from 'vuetify'

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

describe('Component', () => {
  let store
  let vuetify
  let actions
  beforeEach(() => {
    actions = {
        actionClick: jest.fn()
    }
    store = new Vuex.Store({
      actions,
    })
    vuetify = new Vuetify()
  })



  it('method sends value to store when button is clicked', async () => {
    const wrapper = mount(Component, {
      store,
      localVue,
      vuetify,
    })
    wrapper.find('.v-btn').trigger('click')
    expect(actions.actionClick).toHaveBeenCalledWith('buttonComponent/setNewValue', 25)
  })
})

buttonComponent.js:

export const state = () => ({
  value: 0,
})

export const mutations = {
  SET_TO_NEW_VALUE(state, value) {
    state.value = value
  },
}

export const actions = {
  setNewValue({ commit }, value) {
    commit('SET_TO_NEW_VALUE', value)
  },
}
javascript vue.js jestjs vuex nuxt
1个回答
0
投票

只是为了不必在这里再次写它,我将把您链接到我刚刚发表的一篇文章,其中介绍了整个安装过程,以便您可以使用Jest测试Nuxt商店:https://medium.com/@brandonaaskov/how-to-test-nuxt-stores-with-jest-9a5d55d54b28

© www.soinside.com 2019 - 2024. All rights reserved.