Testing a NUXT.js and Vue.js app with Jest. Getting #39;[vuex] module namespace not found in mapState()#39; and #39;[vuex] unknown action type#39;(使用Jest测试NUXT.js和Vue.js应用程序。获取#39;[vuex]模块命名空间在mapState()#39;和#39;[vuex]未知操作类型#39;中找不到) - IT屋-程序员软件
问题描述
尽管我的理解是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)
},
}
推荐答案
这样我就不必在这里再次编写它了,我会将您链接到我刚刚发布的一篇文章,该文章介绍了安装过程,以便您可以使用JEST测试您的Nuxt商店:https://medium.com/@brandonaaskov/how-to-test-nuxt-stores-with-jest-9a5d55d54b28
这篇关于使用Jest测试NUXT.js和Vue.js应用程序。获取';[vuex]模块命名空间在mapState()';和';[vuex]未知操作类型';中找不到的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:使用Jest测试NUXT.js和Vue.js应用程序。获取';[vuex]模块命名空间在mapState()';和';[vuex]未知操作类型';中找不到
基础教程推荐
- 动态更新多个选择框 2022-01-01
- Karma-Jasmine:如何正确监视 Modal? 2022-01-01
- 悬停时滑动输入并停留几秒钟 2022-01-01
- 在 JS 中获取客户端时区(不是 GMT 偏移量) 2022-01-01
- 在for循环中使用setTimeout 2022-01-01
- 角度Apollo设置WatchQuery结果为可用变量 2022-01-01
- 有没有办法使用OpenLayers更改OpenStreetMap中某些要素 2022-09-06
- 当用户滚动离开时如何暂停 youtube 嵌入 2022-01-01
- 响应更改 div 大小保持纵横比 2022-01-01
- 我什么时候应该在导入时使用方括号 2022-01-01