Skip to content

Commit

Permalink
fix: Warn about conflicts between state and module (#1365)
Browse files Browse the repository at this point in the history
* feat: warn about conflicts between state and module

* fix: show module path when it overwrite a state field
  • Loading branch information
simplesmiler authored and ktsn committed Nov 10, 2019
1 parent e5ca2d5 commit 538ee58
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 0 deletions.
7 changes: 7 additions & 0 deletions src/store.js
Expand Up @@ -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)
})
}
Expand Down
22 changes: 22 additions & 0 deletions test/unit/modules.spec.js
Expand Up @@ -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: {
Expand Down

0 comments on commit 538ee58

Please sign in to comment.