Vuex: TypeError: Cannot read property '_modulesNamespaceMap' of undefined

Created on 2 Jul 2018  路  9Comments  路  Source: vuejs/vuex

Versions

  • vuex: 3.0.1
  • vue-test-utils: 1.0.0-beta.18
  • vue-jest: 2.6.0
  • vue-template-compiler: 2.5.16
  • vue: 2.5.16
  • jest: 23.2.0

Reproduction link

https://github.com/Prior99/vuex-crash-reproduce

Steps to reproduce

  1. Clone reproduction repository
  2. Install dependencies using yarn
  3. Run the unit tests using yarn test

What is expected?

The unit tests should pass.

What is actually happening?

The unit tests fail with:

    TypeError: Cannot read property '_modulesNamespaceMap' of undefined
        at getModuleByNamespace (/home/prior/repositories/kn/jest-vue-reproduce/node_modules/vuex/dist/vuex.common.js:914:22)
        at VueComponent.mappedGetter (/home/prior/repositories/kn/jest-vue-reproduce/node_modules/vuex/dist/vuex.common.js:847:25)
        at Watcher.get (/home/prior/repositories/kn/jest-vue-reproduce/node_modules/vue/dist/vue.runtime.common.js:3140:25)
        at Watcher.evaluate (/home/prior/repositories/kn/jest-vue-reproduce/node_modules/vue/dist/vue.runtime.common.js:3247:21)
        at Proxy.computedGetter (/home/prior/repositories/kn/jest-vue-reproduce/node_modules/vue/dist/vue.runtime.common.js:3505:17)
        at Proxy.render (/home/prior/repositories/kn/jest-vue-reproduce/src/some-component.vue:726:50)
        at VueComponent.Vue._render (/home/prior/repositories/kn/jest-vue-reproduce/node_modules/vue/dist/vue.runtime.common.js:4542:22)
        at VueComponent.updateComponent (/home/prior/repositories/kn/jest-vue-reproduce/node_modules/vue/dist/vue.runtime.common.js:2786:21)
        at Watcher.get (/home/prior/repositories/kn/jest-vue-reproduce/node_modules/vue/dist/vue.runtime.common.js:3140:25)
        at new Watcher (/home/prior/repositories/kn/jest-vue-reproduce/node_modules/vue/dist/vue.runtime.common.js:3129:12)
        at mountComponent (/home/prior/repositories/kn/jest-vue-reproduce/node_modules/vue/dist/vue.runtime.common.js:2793:3)
        at VueComponent.Object.<anonymous>.Vue.$mount (/home/prior/repositories/kn/jest-vue-reproduce/node_modules/vue/dist/vue.runtime.common.js:7997:10)
        at init (/home/prior/repositories/kn/jest-vue-reproduce/node_modules/vue/dist/vue.runtime.common.js:4135:13)
        at createComponent (/home/prior/repositories/kn/jest-vue-reproduce/node_modules/vue/dist/vue.runtime.common.js:5606:9)
        at createElm (/home/prior/repositories/kn/jest-vue-reproduce/node_modules/vue/dist/vue.runtime.common.js:5553:9)
        at createChildren (/home/prior/repositories/kn/jest-vue-reproduce/node_modules/vue/dist/vue.runtime.common.js:5680:9)
        at createElm (/home/prior/repositories/kn/jest-vue-reproduce/node_modules/vue/dist/vue.runtime.common.js:5582:9)
        at VueComponent.patch [as __patch__] (/home/prior/repositories/kn/jest-vue-reproduce/node_modules/vue/dist/vue.runtime.common.js:6089:7)
        at VueComponent.Vue._update (/home/prior/repositories/kn/jest-vue-reproduce/node_modules/vue/dist/vue.runtime.common.js:2658:19)
        at VueComponent.updateComponent (/home/prior/repositories/kn/jest-vue-reproduce/node_modules/vue/dist/vue.runtime.common.js:2786:10)
        at Watcher.get (/home/prior/repositories/kn/jest-vue-reproduce/node_modules/vue/dist/vue.runtime.common.js:3140:25)
        at new Watcher (/home/prior/repositories/kn/jest-vue-reproduce/node_modules/vue/dist/vue.runtime.common.js:3129:12)
        at mountComponent (/home/prior/repositories/kn/jest-vue-reproduce/node_modules/vue/dist/vue.runtime.common.js:2793:3)
        at VueComponent.Object.<anonymous>.Vue.$mount (/home/prior/repositories/kn/jest-vue-reproduce/node_modules/vue/dist/vue.runtime.common.js:7997:10)
        at init (/home/prior/repositories/kn/jest-vue-reproduce/node_modules/vue/dist/vue.runtime.common.js:4135:13)
        at createComponent (/home/prior/repositories/kn/jest-vue-reproduce/node_modules/vue/dist/vue.runtime.common.js:5606:9)
        at createElm (/home/prior/repositories/kn/jest-vue-reproduce/node_modules/vue/dist/vue.runtime.common.js:5553:9)
        at VueComponent.patch [as __patch__] (/home/prior/repositories/kn/jest-vue-reproduce/node_modules/vue/dist/vue.runtime.common.js:6089:7)
        at VueComponent.Vue._update (/home/prior/repositories/kn/jest-vue-reproduce/node_modules/vue/dist/vue.runtime.common.js:2658:19)
        at VueComponent.updateComponent (/home/prior/repositories/kn/jest-vue-reproduce/node_modules/vue/dist/vue.runtime.common.js:2786:10)
        at Watcher.get (/home/prior/repositories/kn/jest-vue-reproduce/node_modules/vue/dist/vue.runtime.common.js:3140:25)
        at new Watcher (/home/prior/repositories/kn/jest-vue-reproduce/node_modules/vue/dist/vue.runtime.common.js:3129:12)
        at mountComponent (/home/prior/repositories/kn/jest-vue-reproduce/node_modules/vue/dist/vue.runtime.common.js:2793:3)
        at VueComponent.Object.<anonymous>.Vue.$mount (/home/prior/repositories/kn/jest-vue-reproduce/node_modules/vue/dist/vue.runtime.common.js:7997:10)
        at Object.mount (/home/prior/repositories/kn/jest-vue-reproduce/node_modules/@vue/test-utils/dist/vue-test-utils.js:5940:21)
        at Object.<anonymous> (/home/prior/repositories/kn/jest-vue-reproduce/src/__tests__/test-something.ts:19:21)
        at Object.asyncFn (/home/prior/repositories/kn/jest-vue-reproduce/node_modules/jest-jasmine2/build/jasmine_async.js:108:37)
        at resolve (/home/prior/repositories/kn/jest-vue-reproduce/node_modules/jest-jasmine2/build/queue_runner.js:56:12)
        at new Promise (<anonymous>)
        at mapper (/home/prior/repositories/kn/jest-vue-reproduce/node_modules/jest-jasmine2/build/queue_runner.js:43:19)
        at promise.then (/home/prior/repositories/kn/jest-vue-reproduce/node_modules/jest-jasmine2/build/queue_runner.js:87:41)
        at <anonymous>
        at process._tickCallback (internal/process/next_tick.js:188:7)

When changing the store module not to use namespaces by removing the

                namespaced: true,

in both src/__tests__/test-something.ts and src/store.ts and changing the line

        ...mapGetters("myModule", ["getContent"]),

to

        ...mapGetters(["getContent"]),

in src/some-component.vue the error changes to:

    TypeError: Cannot read property 'getters' of undefined
        at VueComponent.mappedGetter (/home/prior/repositories/kn/jest-vue-reproduce/node_modules/vuex/dist/vuex.common.js:850:73)
        at Watcher.get (/home/prior/repositories/kn/jest-vue-reproduce/node_modules/vue/dist/vue.runtime.common.js:3140:25)
        at Watcher.evaluate (/home/prior/repositories/kn/jest-vue-reproduce/node_modules/vue/dist/vue.runtime.common.js:3247:21)
        at Proxy.computedGetter (/home/prior/repositories/kn/jest-vue-reproduce/node_modules/vue/dist/vue.runtime.common.js:3505:17)
        at Proxy.render (/home/prior/repositories/kn/jest-vue-reproduce/src/some-component.vue:726:50)
        at VueComponent.Vue._render (/home/prior/repositories/kn/jest-vue-reproduce/node_modules/vue/dist/vue.runtime.common.js:4542:22)
        at VueComponent.updateComponent (/home/prior/repositories/kn/jest-vue-reproduce/node_modules/vue/dist/vue.runtime.common.js:2786:21)
        at Watcher.get (/home/prior/repositories/kn/jest-vue-reproduce/node_modules/vue/dist/vue.runtime.common.js:3140:25)
        at new Watcher (/home/prior/repositories/kn/jest-vue-reproduce/node_modules/vue/dist/vue.runtime.common.js:3129:12)
        at mountComponent (/home/prior/repositories/kn/jest-vue-reproduce/node_modules/vue/dist/vue.runtime.common.js:2793:3)
        at VueComponent.Object.<anonymous>.Vue.$mount (/home/prior/repositories/kn/jest-vue-reproduce/node_modules/vue/dist/vue.runtime.common.js:7997:10)
        at init (/home/prior/repositories/kn/jest-vue-reproduce/node_modules/vue/dist/vue.runtime.common.js:4135:13)
        at createComponent (/home/prior/repositories/kn/jest-vue-reproduce/node_modules/vue/dist/vue.runtime.common.js:5606:9)
        at createElm (/home/prior/repositories/kn/jest-vue-reproduce/node_modules/vue/dist/vue.runtime.common.js:5553:9)
        at createChildren (/home/prior/repositories/kn/jest-vue-reproduce/node_modules/vue/dist/vue.runtime.common.js:5680:9)
        at createElm (/home/prior/repositories/kn/jest-vue-reproduce/node_modules/vue/dist/vue.runtime.common.js:5582:9)
        at VueComponent.patch [as __patch__] (/home/prior/repositories/kn/jest-vue-reproduce/node_modules/vue/dist/vue.runtime.common.js:6089:7)
        at VueComponent.Vue._update (/home/prior/repositories/kn/jest-vue-reproduce/node_modules/vue/dist/vue.runtime.common.js:2658:19)
        at VueComponent.updateComponent (/home/prior/repositories/kn/jest-vue-reproduce/node_modules/vue/dist/vue.runtime.common.js:2786:10)
        at Watcher.get (/home/prior/repositories/kn/jest-vue-reproduce/node_modules/vue/dist/vue.runtime.common.js:3140:25)
        at new Watcher (/home/prior/repositories/kn/jest-vue-reproduce/node_modules/vue/dist/vue.runtime.common.js:3129:12)
        at mountComponent (/home/prior/repositories/kn/jest-vue-reproduce/node_modules/vue/dist/vue.runtime.common.js:2793:3)
        at VueComponent.Object.<anonymous>.Vue.$mount (/home/prior/repositories/kn/jest-vue-reproduce/node_modules/vue/dist/vue.runtime.common.js:7997:10)
        at init (/home/prior/repositories/kn/jest-vue-reproduce/node_modules/vue/dist/vue.runtime.common.js:4135:13)
        at createComponent (/home/prior/repositories/kn/jest-vue-reproduce/node_modules/vue/dist/vue.runtime.common.js:5606:9)
        at createElm (/home/prior/repositories/kn/jest-vue-reproduce/node_modules/vue/dist/vue.runtime.common.js:5553:9)
        at VueComponent.patch [as __patch__] (/home/prior/repositories/kn/jest-vue-reproduce/node_modules/vue/dist/vue.runtime.common.js:6089:7)
        at VueComponent.Vue._update (/home/prior/repositories/kn/jest-vue-reproduce/node_modules/vue/dist/vue.runtime.common.js:2658:19)
        at VueComponent.updateComponent (/home/prior/repositories/kn/jest-vue-reproduce/node_modules/vue/dist/vue.runtime.common.js:2786:10)
        at Watcher.get (/home/prior/repositories/kn/jest-vue-reproduce/node_modules/vue/dist/vue.runtime.common.js:3140:25)
        at new Watcher (/home/prior/repositories/kn/jest-vue-reproduce/node_modules/vue/dist/vue.runtime.common.js:3129:12)
        at mountComponent (/home/prior/repositories/kn/jest-vue-reproduce/node_modules/vue/dist/vue.runtime.common.js:2793:3)
        at VueComponent.Object.<anonymous>.Vue.$mount (/home/prior/repositories/kn/jest-vue-reproduce/node_modules/vue/dist/vue.runtime.common.js:7997:10)
        at Object.mount (/home/prior/repositories/kn/jest-vue-reproduce/node_modules/@vue/test-utils/dist/vue-test-utils.js:5940:21)
        at Object.<anonymous> (/home/prior/repositories/kn/jest-vue-reproduce/src/__tests__/test-something.ts:19:21)
        at Object.asyncFn (/home/prior/repositories/kn/jest-vue-reproduce/node_modules/jest-jasmine2/build/jasmine_async.js:108:37)
        at resolve (/home/prior/repositories/kn/jest-vue-reproduce/node_modules/jest-jasmine2/build/queue_runner.js:56:12)
        at new Promise (<anonymous>)
        at mapper (/home/prior/repositories/kn/jest-vue-reproduce/node_modules/jest-jasmine2/build/queue_runner.js:43:19)
        at promise.then (/home/prior/repositories/kn/jest-vue-reproduce/node_modules/jest-jasmine2/build/queue_runner.js:87:41)
        at <anonymous>
        at process._tickCallback (internal/process/next_tick.js:188:7)

Please note that the unit test is not failing with these errors. They are logged from inside vue.runtime.common.js.
The actual error with which the unit tests fail is:

    TypeError: Cannot read property 'getters' of undefined

      at VueComponent.mappedGetter (node_modules/vuex/dist/vuex.common.js:850:73)
      at Proxy.render (src/some-component.vue:726:50)
      at Object.<anonymous> (src/__tests__/test-something.ts:19:21)

Hints

  • When removing Vue.extend in src/some-component.vue the failing test passes.
  • The error only occurs when the SomeComponent component is used inside a parent component, hence the test in src/__tests__/test-some-component.ts test is passing.
  • This also happens with version 2.x.x of vuex and older versions of jest and vue.
  • This issue also happens when not using local vue.

This issue might be related to other issues across the web:

Most helpful comment

so, how to resolve this problem?

All 9 comments

We did some further investigations and came to the conclusion that the beforeCreate hook with which Vuex registers the $store property on all components is not called early enough. I created another issue in vue: vue#8441. This might also solve this issue.

This is because the component in the actual source code and the instance in the test code reference different Vue constructor. We cannot do anything for this in Vuex side...

@ktsn Thanks for your quick response and very helpful answer.

so, how to resolve this problem?

This is already fixed on vue-test-utils.

I'm still getting this issue with the latest verison of test utils (at time of writing).

This is already fixed on vue-test-utils.

could you give us more detail about it

It is not fixed because no example for it

  1. Use the latest vue-test-utils.
  2. Make sure you inject localVue in your mount option. https://vue-test-utils.vuejs.org/api/options.html#localvue

Please consider to use forum or chat before asking your question in already closed issue as it is not a support forum. The maintainers tend to not respond as it is marked as solved already. If it still happens on your side by other cause, please open a new issue.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

Ge-yuan-jun picture Ge-yuan-jun  路  3Comments

jbruni picture jbruni  路  3Comments

james-wasson picture james-wasson  路  3Comments

matthewmorgan picture matthewmorgan  路  3Comments

haoxins picture haoxins  路  4Comments