Skip to content

Commit 62e05cc

Browse files
committed
Merge branch 'warn-about-conflicts' of github.com:simplesmiler/vuex into simplesmiler-warn-about-conflicts
2 parents e5ca2d5 + db96c45 commit 62e05cc

File tree

2 files changed

+25
-0
lines changed

2 files changed

+25
-0
lines changed

src/store.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -315,6 +315,13 @@ function installModule (store, rootState, path, module, hot) {
315315
const parentState = getNestedState(rootState, path.slice(0, -1))
316316
const moduleName = path[path.length - 1]
317317
store._withCommit(() => {
318+
if (process.env.NODE_ENV !== 'production') {
319+
if (moduleName in parentState) {
320+
console.warn(
321+
`[vuex] state field "${moduleName}" was overridden by a module with the same name`
322+
)
323+
}
324+
}
318325
Vue.set(parentState, moduleName, module.state)
319326
})
320327
}

test/unit/modules.spec.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -547,6 +547,24 @@ describe('Modules', () => {
547547
store.dispatch('parent/test')
548548
})
549549

550+
it('module: warn when module overrides state', () => {
551+
spyOn(console, 'warn')
552+
const store = new Vuex.Store({
553+
state () {
554+
return { value: 1 }
555+
},
556+
modules: {
557+
value: {
558+
state: () => 2
559+
}
560+
}
561+
})
562+
expect(store.state.value).toBe(2)
563+
expect(console.warn).toHaveBeenCalledWith(
564+
`[vuex] state field "value" was overridden by a module with the same name`
565+
)
566+
})
567+
550568
it('dispatching multiple actions in different modules', done => {
551569
const store = new Vuex.Store({
552570
modules: {

0 commit comments

Comments
 (0)