简体   繁体   English

TypeError:无法读取 Vue 中未定义的属性“_modulesNamespaceMap”

[英]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.htmlhttps://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.

相关问题 Vue TypeError:无法读取未定义的属性“loggedIn” - Vue TypeError: Cannot read property 'loggedIn' of undefined Nativescript Vue TypeError:无法读取未定义的属性“ 0” - Nativescript Vue TypeError: Cannot read property '0' of undefined Vue TypeError:无法读取未定义的属性“_wrapper” - Vue TypeError: Cannot read property '_wrapper' of undefined 获取Vue警告TypeError:无法读取未定义的属性,但存在数据 - Getting a Vue warning TypeError: Cannot read property of undefined but the data is present vue-socket.io 未捕获的类型错误:无法读取未定义的属性 - vue-socket.io Uncaught TypeError: Cannot read property of undefined nextTick中的错误:Vue中的“ TypeError:无法读取未定义的属性'key'” - Error in nextTick: “TypeError: Cannot read property 'key' of undefined” in vue [Vue 警告]:渲染错误:“TypeError:无法读取未定义的属性‘文本’” - [Vue warn]: Error in render: “TypeError: Cannot read property 'text' of undefined” TypeError:无法读取未定义vue js的属性'get' - TypeError: Cannot read property 'get' of undefined vue js Vue路由器:TypeError:无法读取未定义的属性“ $ createElement” - Vue router : TypeError: Cannot read property '$createElement' of undefined [Vue警告]:渲染错误:“ TypeError:无法读取未定义的属性'replace'” - [Vue warn]: Error in render: “TypeError: Cannot read property 'replace' of undefined”
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM