Skip to content

Commit

Permalink
feat: warn about conflicts between state and module
Browse files Browse the repository at this point in the history
  • Loading branch information
simplesmiler committed Aug 3, 2018
1 parent 3a5e530 commit db96c45
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 0 deletions.
7 changes: 7 additions & 0 deletions src/store.js
Original file line number Diff line number Diff line change
Expand Up @@ -282,6 +282,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`
)
}
}
Vue.set(parentState, moduleName, module.state)
})
}
Expand Down
18 changes: 18 additions & 0 deletions test/unit/modules.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -547,6 +547,24 @@ describe('Modules', () => {
store.dispatch('parent/test')
})

it('module: warn when module overrides state', () => {
spyOn(console, 'warn')
const store = new Vuex.Store({
state () {
return { value: 1 }
},
modules: {
value: {
state: () => 2
}
}
})
expect(store.state.value).toBe(2)
expect(console.warn).toHaveBeenCalledWith(
`[vuex] state field "value" was overridden by a module with the same name`
)
})

it('dispatching multiple actions in different modules', done => {
const store = new Vuex.Store({
modules: {
Expand Down

0 comments on commit db96c45

Please sign in to comment.