[英]TypeError: Cannot read property '_modulesNamespaceMap' of undefined in Vue
I have a simple webapp created to practice my testing skills in vue using Vue Test Utils and Jest but come up with an error regarding with Vue.我创建了一个简单的 web 应用程序,用于使用 Vue Test Utils 和 Jest 练习我在 Vue 中的测试技能,但出现了关于 Vue 的错误。 I'm just console logging to see if my AddDialog is in my Home file but have an error: This is my error:
我只是控制台日志,看看我的 AddDialog 是否在我的主文件中但有一个错误:这是我的错误:
TypeError: Cannot read property '_modulesNamespaceMap' of undefined
TypeError:无法读取未定义的属性“_modulesNamespaceMap”
Here is my testing code.这是我的测试代码。
// Imports
import Home from '@/components/Home'
// Utilities
import {createLocalVue, mount, } from '@vue/test-utils'
import Vue from 'vue'
import Vuetify from 'vuetify'
import AddDialog from "@/components/AddDialog";
Vue.use(Vuetify)
describe('Home.vue', () => {
const localVue = createLocalVue()
let vuetify
beforeEach(() => {
vuetify = new Vuetify()
})
test('creates a todo', async () => {
const wrapper = mount(Home)
console.log(wrapper)
})
})
Because inside Home using store, but you render without it.因为在 Home 内部使用 store,但你没有它渲染。https://vue-test-utils.vuejs.org/guides/using-with-vuex.html
https://vue-test-utils.vuejs.org/guides/using-with-vuex.html
As correctly pointed out by @Raynhour, the problem is when store
is not being added to the test.正如@Raynhour 正确指出的那样,问题在于没有将
store
添加到测试中。 In addition to this response, I'll add a sample code snippet, which fixed the TypeError: Cannot read property '_modulesNamespaceMap' of undefined
in one of my components using Vuex
.除了这个响应,我将添加一个示例代码片段,它修复了
TypeError: Cannot read property '_modulesNamespaceMap' of undefined
在我的一个组件中使用Vuex
。
describe("MyComponent.vue", () => {
let actions: any;
let getters: any;
let state: any;
let store: any;
let wrapper: any;
beforeEach(() => {
actions = {
sampleAction: jest.fn(),
};
getters = {
sampleGetter: jest.fn(),
};
state = {
sampleState: jest.fn(),
};
store = new Vuex.Store({
modules: {
requests: {
namespaced: true,
actions,
getters,
state,
},
},
});
});
wrapper = shallowMount(MyComponent, {
store,
});
... // Some test cases...
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.