diff --git a/src/store.js b/src/store.js index 039e18165..00caafabc 100644 --- a/src/store.js +++ b/src/store.js @@ -315,6 +315,13 @@ function installModule (store, rootState, path, module, hot) { const parentState = getNestedState(rootState, path.slice(0, -1)) const moduleName = path[path.length - 1] store._withCommit(() => { + if (process.env.NODE_ENV !== 'production') { + if (moduleName in parentState) { + console.warn( + `[vuex] state field "${moduleName}" was overridden by a module with the same name at "${path.join('.')}"` + ) + } + } Vue.set(parentState, moduleName, module.state) }) } diff --git a/test/unit/modules.spec.js b/test/unit/modules.spec.js index a776fb582..8d4b78904 100644 --- a/test/unit/modules.spec.js +++ b/test/unit/modules.spec.js @@ -547,6 +547,28 @@ describe('Modules', () => { store.dispatch('parent/test') }) + it('module: warn when module overrides state', () => { + spyOn(console, 'warn') + const store = new Vuex.Store({ + modules: { + foo: { + state () { + return { value: 1 } + }, + modules: { + value: { + state: () => 2 + } + } + } + } + }) + expect(store.state.foo.value).toBe(2) + expect(console.warn).toHaveBeenCalledWith( + `[vuex] state field "value" was overridden by a module with the same name at "foo.value"` + ) + }) + it('dispatching multiple actions in different modules', done => { const store = new Vuex.Store({ modules: {