Jest 单元测试 store里 'getters' 未定义
Jest 单元测试,如果写vuex 相关的 store 文件如何解决 Test suite failed to run TypeError: Cannot read property 'getters' of undefined 问题
aaa.spec.js代码
import { createLocalVue } from '@vue/test-utils'
import Vuex from 'vuex'
import storeConfig from '@/store/modules/user'
import { cloneDeep } from 'lodash'
import * as userApi from '@/api/user'
const { mutations, state, actions } = storeConfig
let store
beforeEach(() => {
const localVue = createLocalVue
localVue.use(Vuex)
store = new Vuex.Store(cloneDeep(storeConfig))
})
describe('test of vuex', () => {
store.commit('SET_NAME', 'Li')
expect(store.state.name).toBe('Li')
})
})
store/index.js 代码
import Vue from 'vue'
import Vuex from 'vuex'
import getters from './getters'
import app from './modules/app'
Vue.use(Vuex)
const store = new Vuex.Store({
getters,
modules: {
app
},
})
export default store
执行命令npm run test:unit
报错信息
`
FAIL tests/unit/store/aaa.spec.js
● Test suite failed to run
TypeError: Cannot read property 'getters' of undefined
9 | Vue.use(Vuex)
10 |
> 11 | const store = new Vuex.Store({
| ^
12 | getters,
13 | modules: {
14 | app,
at node_modules/vuex/dist/vuex.common.js:272:19
at Array.forEach (<anonymous>)
at assertRawModule (node_modules/vuex/dist/vuex.common.js:271:28)
at ModuleCollection.register (node_modules/vuex/dist/vuex.common.js:197:5)
at node_modules/vuex/dist/vuex.common.js:211:14
at node_modules/vuex/dist/vuex.common.js:80:52
at Array.forEach (<anonymous>)
at forEachValue (node_modules/vuex/dist/vuex.common.js:80:20)
at ModuleCollection.register (node_modules/vuex/dist/vuex.common.js:210:5)
at new ModuleCollection (node_modules/vuex/dist/vuex.common.js:171:8)
at new Store (node_modules/vuex/dist/vuex.common.js:322:19)
at Object.<anonymous> (src/store/index.js:11:15)
at Object.<anonymous> (src/utils/request.js:8:1)
at Object.<anonymous> (src/api/user.js:1:1)
at Object.<anonymous> (src/store/modules/user.js:1:1)
at Object.<anonymous> (tests/unit/store/user.spec.js:3:1)`
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论