【问题标题】:Testing a NUXT.js and Vue.js app with Jest. Getting '[vuex] module namespace not found in mapState()' and '[vuex] unknown action type'使用 Jest 测试 NUXT.js 和 Vue.js 应用程序。获取“在 mapState() 中找不到 [vuex] 模块命名空间”和“[vuex] 未知操作类型”
【发布时间】:2020-05-21 23:05:09
【问题描述】:

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

下面是组件、存储和测试的代码。

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.js


    【解决方案1】:

    为了不必在这里再写一遍,我会将您链接到我刚刚发布的一篇文章,该文章介绍了设置过程,以便您可以使用 Jest 测试您的 Nuxt 商店:https://medium.com/@brandonaaskov/how-to-test-nuxt-stores-with-jest-9a5d55d54b28

    【讨论】:

    • 使用 nuxt builder 在我的机器上运行 1 次测试大约需要 5 秒。更好的选择是模拟 nuxt 商店。
    • @ioan 在你自己的代码库中模拟行为是一个糟糕的主意。应该有更好的方法来做到这一点。
    猜你喜欢
    • 2019-01-21
    • 2020-03-29
    • 2019-03-13
    • 2021-05-04
    • 2018-08-17
    • 2021-11-28
    • 2018-01-17
    • 1970-01-01
    相关资源
    最近更新 更多